rprogram 0.2.3 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
1
  module RProgram
2
2
  # Version of RProgram
3
- VERSION = '0.2.3'
3
+ VERSION = '0.3.0'
4
4
  end
data/rprogram.gemspec CHANGED
@@ -9,7 +9,7 @@ rescue NameError
9
9
  require 'ore/specification'
10
10
  retry
11
11
  rescue LoadError
12
- STDERR.puts "The 'rprogram.gemspec' file requires Ore."
12
+ STDERR.puts "The '#{__FILE__}' file requires Ore."
13
13
  STDERR.puts "Run `gem install ore-core` to install Ore."
14
14
  end
15
15
  end
@@ -1,8 +1,4 @@
1
- require 'rprogram/nameable'
2
-
3
- class AliasedProgram
4
-
5
- include RProgram::Nameable
1
+ class AliasedProgram < RProgram::Program
6
2
 
7
3
  name_program 'ls'
8
4
  alias_program 'dir'
@@ -1,8 +1,4 @@
1
- require 'rprogram/nameable'
2
-
3
- class NamedProgram
4
-
5
- include RProgram::Nameable
1
+ class NamedProgram < RProgram::Program
6
2
 
7
3
  name_program 'ls'
8
4
 
data/spec/program_spec.rb CHANGED
@@ -1,12 +1,95 @@
1
1
  require 'rprogram/program'
2
2
 
3
3
  require 'spec_helper'
4
+ require 'classes/named_program'
5
+ require 'classes/aliased_program'
4
6
  require 'classes/ls_program'
5
7
 
6
8
  describe Program do
7
9
  subject { Program.new('/usr/bin/cc') }
8
10
  after(:all) { LS.path = nil }
9
11
 
12
+ describe "named program" do
13
+ subject { NamedProgram }
14
+
15
+ it "should be able to give a class a program name" do
16
+ subject.program_name.should == 'ls'
17
+ end
18
+
19
+ it "should not have any program aliases" do
20
+ subject.program_aliases.should be_empty
21
+ end
22
+
23
+ it "should have one program name" do
24
+ subject.program_names.should == ['ls']
25
+ end
26
+
27
+ it "should provide an instance method for the program name" do
28
+ program = subject.find
29
+
30
+ program.program_name.should == 'ls'
31
+ end
32
+
33
+ it "should provide an instance method for the program names" do
34
+ program = subject.find
35
+
36
+ program.program_names.should == ['ls']
37
+ end
38
+ end
39
+
40
+ describe "aliased program" do
41
+ subject { AliasedProgram }
42
+
43
+ it "should have program aliases" do
44
+ subject.program_aliases.should == ['dir']
45
+ end
46
+
47
+ it "should have one program name" do
48
+ subject.program_names.should == ['ls', 'dir']
49
+ end
50
+
51
+ it "should provide an instance method for the program aliases" do
52
+ program = subject.find
53
+
54
+ program.program_aliases.should == ['dir']
55
+ end
56
+
57
+ it "should provide an instance method for the program names" do
58
+ program = subject.find
59
+
60
+ program.program_names.should == ['ls', 'dir']
61
+ end
62
+ end
63
+
64
+ describe "path" do
65
+ subject { NamedProgram }
66
+
67
+ it "should not have a path by default" do
68
+ subject.path.should be_nil
69
+ end
70
+
71
+ it "should allow setting the path" do
72
+ new_path = '/bin/ls'
73
+
74
+ subject.path = new_path
75
+ subject.path.should == new_path
76
+ end
77
+
78
+ it "should expand paths" do
79
+ subject.path = '/../bin/ls'
80
+
81
+ subject.path.should == '/bin/ls'
82
+ end
83
+
84
+ it "should allow setting the path to nil" do
85
+ subject.path = nil
86
+
87
+ subject.path.should be_nil
88
+ end
89
+
90
+ after(:all) { NamedProgram.path = nil }
91
+ end
92
+
10
93
  it "should create a Program from a path" do
11
94
  subject.should_not be_nil
12
95
  end
@@ -50,7 +133,7 @@ describe Program do
50
133
  end
51
134
 
52
135
  it "should allow using a default path" do
53
- LS.path = '/usr/bin/dir'
136
+ LS.path = '/bin/ls'
54
137
 
55
138
  LS.find.path.should == LS.path
56
139
  end
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ puts STDIN.readline.chomp
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ exit -1
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ puts ARGV[0]
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ exit 0
@@ -0,0 +1,80 @@
1
+ require 'spec_helper'
2
+ require 'tempfile'
3
+
4
+ require 'rprogram/system'
5
+
6
+ describe System do
7
+ subject { System }
8
+
9
+ it "should determine the native architecture" do
10
+ subject.arch.should_not be_empty
11
+ end
12
+
13
+ it "should determine the native platform" do
14
+ subject.platform.should_not be_empty
15
+ end
16
+
17
+ it "should have a list of directories that contain programs" do
18
+ subject.paths.should_not be_empty
19
+
20
+ subject.paths.any? { |dir| dir.directory? }.should == true
21
+ end
22
+
23
+ it "should be able to find programs" do
24
+ subject.find_program('ls').should be_executable
25
+ end
26
+
27
+ it "should be able to find programs by multiple names" do
28
+ subject.find_program_by_names('ls','dir').should be_executable
29
+ end
30
+
31
+ describe "run" do
32
+ let(:scripts_dir) { File.join(File.dirname(__FILE__),'scripts') }
33
+
34
+ let(:fail_script) { File.join(scripts_dir,'fail.rb') }
35
+ let(:success_script) { File.join(scripts_dir,'success.rb') }
36
+
37
+ let(:print_script) { File.join(scripts_dir,'print.rb') }
38
+ let(:echo_script) { File.join(scripts_dir,'echo.rb') }
39
+ let(:data) { 'hello' }
40
+
41
+ it "should return true when programs succeed" do
42
+ subject.run(success_script).should == true
43
+ end
44
+
45
+ it "should return false when programs fail" do
46
+ subject.run(fail_script).should == false
47
+ end
48
+
49
+ unless System.ruby_1_8?
50
+ it "should allow passing exec options as the last argument" do
51
+ output = Tempfile.new('rprogram_run_with_options')
52
+ subject.run(print_script, data, :out => [output.path, 'w'])
53
+
54
+ output.read.chomp.should == data
55
+ end
56
+ else
57
+ it "should raise an exception when passing exec options" do
58
+ lambda {
59
+ subject.run(print_script, data, :out => ['foo', 'w'])
60
+ }.should raise_error
61
+ end
62
+ end
63
+
64
+
65
+ unless (System.ruby_1_8? || (System.windows? && !System.jruby?))
66
+ it "should allow running programs with IO.popen" do
67
+ io = subject.run(echo_script, :popen => 'w+')
68
+
69
+ io.puts(data)
70
+ io.readline.chomp.should == data
71
+ end
72
+ else
73
+ it "should raise an exception when specifying :popen" do
74
+ lambda {
75
+ subject.run(echo_script, :popen => 'w+')
76
+ }.should raise_error
77
+ end
78
+ end
79
+ end
80
+ end
data/spec/task_spec.rb CHANGED
@@ -35,21 +35,6 @@ describe Task do
35
35
  end
36
36
  end
37
37
 
38
- describe "sudo" do
39
- it "should allow passing in the :sudo option" do
40
- task = LSTask.new(:sudo => true)
41
-
42
- task.should be_sudo
43
- end
44
-
45
- it "should allow enabling sudo after the task has been initialized" do
46
- task = LSTask.new
47
-
48
- task.sudo = true
49
- task.should be_sudo
50
- end
51
- end
52
-
53
38
  before(:each) do
54
39
  @task = LSTask.new
55
40
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: rprogram
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.3
5
+ version: 0.3.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Postmodern
@@ -10,8 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-03-30 00:00:00 -04:00
14
- default_executable:
13
+ date: 2011-04-08 00:00:00 Z
15
14
  dependencies:
16
15
  - !ruby/object:Gem::Dependency
17
16
  name: env
@@ -79,21 +78,16 @@ files:
79
78
  - Rakefile
80
79
  - gemspec.yml
81
80
  - lib/rprogram.rb
82
- - lib/rprogram/compat.rb
83
81
  - lib/rprogram/exceptions.rb
84
82
  - lib/rprogram/exceptions/program_not_found.rb
85
- - lib/rprogram/extensions.rb
86
- - lib/rprogram/nameable.rb
87
- - lib/rprogram/nameable/class_methods.rb
88
- - lib/rprogram/nameable/nameable.rb
89
83
  - lib/rprogram/non_option.rb
90
84
  - lib/rprogram/option.rb
91
85
  - lib/rprogram/option_list.rb
92
- - lib/rprogram/options.rb
93
- - lib/rprogram/options/class_methods.rb
94
- - lib/rprogram/options/options.rb
95
86
  - lib/rprogram/program.rb
96
87
  - lib/rprogram/rprogram.rb
88
+ - lib/rprogram/sudo.rb
89
+ - lib/rprogram/sudo_task.rb
90
+ - lib/rprogram/system.rb
97
91
  - lib/rprogram/task.rb
98
92
  - lib/rprogram/version.rb
99
93
  - lib/rprogram/yard.rb
@@ -103,17 +97,19 @@ files:
103
97
  - spec/classes/ls_selinux_task.rb
104
98
  - spec/classes/ls_task.rb
105
99
  - spec/classes/named_program.rb
106
- - spec/compat_spec.rb
107
- - spec/nameable_spec.rb
108
100
  - spec/non_option_spec.rb
109
101
  - spec/option_examples.rb
110
102
  - spec/option_list_spec.rb
111
103
  - spec/option_spec.rb
112
104
  - spec/program_spec.rb
113
105
  - spec/rprogram_spec.rb
106
+ - spec/scripts/echo.rb
107
+ - spec/scripts/fail.rb
108
+ - spec/scripts/print.rb
109
+ - spec/scripts/success.rb
114
110
  - spec/spec_helper.rb
111
+ - spec/system_spec.rb
115
112
  - spec/task_spec.rb
116
- has_rdoc: yard
117
113
  homepage: http://github.com/postmodern/rprogram
118
114
  licenses:
119
115
  - MIT
@@ -137,16 +133,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
137
133
  requirements: []
138
134
 
139
135
  rubyforge_project: rprogram
140
- rubygems_version: 1.6.2
136
+ rubygems_version: 1.7.2
141
137
  signing_key:
142
138
  specification_version: 3
143
139
  summary: A library for creating wrappers around command-line programs.
144
140
  test_files:
141
+ - spec/program_spec.rb
145
142
  - spec/rprogram_spec.rb
146
- - spec/nameable_spec.rb
147
- - spec/option_list_spec.rb
148
143
  - spec/non_option_spec.rb
149
- - spec/task_spec.rb
150
- - spec/compat_spec.rb
151
- - spec/program_spec.rb
144
+ - spec/system_spec.rb
145
+ - spec/option_list_spec.rb
152
146
  - spec/option_spec.rb
147
+ - spec/task_spec.rb
@@ -1,124 +0,0 @@
1
- require 'rprogram/exceptions/program_not_found'
2
- require 'rprogram/rprogram'
3
-
4
- require 'env/variables'
5
-
6
- module RProgram
7
- module Compat
8
- extend Env::Variables
9
-
10
- #
11
- # Determines the native platform.
12
- #
13
- # @return [String]
14
- # The native platform.
15
- #
16
- # @example
17
- # Compat.arch #=> "linux"
18
- #
19
- # @deprecated Will be removed in 0.3.0.
20
- #
21
- def Compat.platform
22
- RUBY_PLATFORM.split('-').last
23
- end
24
-
25
- #
26
- # Finds the full-path of the program with the matching name.
27
- #
28
- # @param [String] name
29
- # The name of the program to find.
30
- #
31
- # @return [Pathname, nil]
32
- # The full-path of the desired program.
33
- #
34
- # @example
35
- # Compat.find_program('as')
36
- # #=> #<Pathname:/usr/bin/as>
37
- #
38
- def Compat.find_program(name)
39
- # add the `.exe` suffix to the name, if running on Windows
40
- if platform =~ /mswin/
41
- name = "#{name}.exe"
42
- end
43
-
44
- paths.each do |dir|
45
- full_path = dir.join(name).expand_path
46
-
47
- return full_path if full_path.file?
48
- end
49
-
50
- return nil
51
- end
52
-
53
- #
54
- # Finds the program matching one of the matching names.
55
- #
56
- # @param [Array] names
57
- # The names of the program to use while searching for the program.
58
- #
59
- # @return [Pathname, nil]
60
- # The first full-path for the program.
61
- #
62
- # @example
63
- # Compat.find_program_by_names("gas","as") #=> "/usr/bin/as"
64
- #
65
- def Compat.find_program_by_names(*names)
66
- names.each do |name|
67
- if (path = find_program(name))
68
- return path
69
- end
70
- end
71
-
72
- return nil
73
- end
74
-
75
- #
76
- # Runs a program.
77
- #
78
- # @param [String] path
79
- # The path to the program.
80
- #
81
- # @param [Array] args
82
- # Additional arguments to run the program with.
83
- #
84
- # @return [Boolean]
85
- # Specifies whether the program exited successfully.
86
- #
87
- def Compat.run(path,*args)
88
- args = args.map { |arg| arg.to_s }
89
-
90
- if RProgram.debug
91
- STDERR.puts ">>> #{path} #{args.join(' ')}"
92
- end
93
-
94
- return Kernel.system(path,*args)
95
- end
96
-
97
- #
98
- # Runs a program under sudo.
99
- #
100
- # @param [String] path
101
- # Path of the program to run.
102
- #
103
- # @param [Array] args
104
- # Additional arguments to run the program with.
105
- #
106
- # @return [Boolean]
107
- # Specifies whether the program exited successfully.
108
- #
109
- # @raise [ProgramNotFound]
110
- # Indicates that the `sudo` program could not be located.
111
- #
112
- # @since 0.1.8
113
- #
114
- def Compat.sudo(path,*args)
115
- sudo_path = find_program('sudo')
116
-
117
- unless sudo_path
118
- raise(ProgramNotFound,'could not find the "sudo" program',caller)
119
- end
120
-
121
- return run(sudo_path,path,*args)
122
- end
123
- end
124
- end