clintegracon 0.5.3 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 66075c5c5f568f363aeb4427edfe784b28658d73
4
- data.tar.gz: 2eebf0522c50cedf00cc06612c8fc3fef1200ffe
3
+ metadata.gz: da046b97927be25951f614f5a1d4fa20feab0520
4
+ data.tar.gz: 5f499a259ddc350ba287541be417d440e69db5c7
5
5
  SHA512:
6
- metadata.gz: 21d257b2d152d77a7c3c63cdf24b10c250b358543e6ff1f0b5b0ae72426a2167f4e405dfa0086c0451f60bb231908d9a62ac7ffb005b48ae0970ac3e5959ac4a
7
- data.tar.gz: bb71dbb097317af4f98322de319adaeb4769c51dd47ea14b23c195ce43333d002e51bf9c28591851db0444bfbba001a35745b583d99198a272674acf0b33c682
6
+ metadata.gz: d65d4fa9ae1a25a3dfd934dd5e5bd20ffe654c9ca773684f3f111a414b6d99381b9c1c5d25e037f8f7c7b25f3a8dc2e0059e8a60e742d26febc0a5b8ca68f3b1
7
+ data.tar.gz: 4faacee342cdfbbb01b4b43ae83fd79cda250c250294d28ca7c1d1e2fd11c4e3a2a62eb3e67243c9082aa18744aa0807d75cb98a0a83707a5fbe134da03f677c
@@ -1,18 +1,22 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- clintegracon (0.5.3)
4
+ clintegracon (0.6.0)
5
5
  colored (~> 1.2)
6
6
  diffy
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
+ activesupport (3.2.20)
12
+ i18n (~> 0.6, >= 0.6.4)
13
+ multi_json (~> 1.0)
11
14
  bacon (1.2.0)
12
15
  claide (0.7.0)
13
16
  coderay (1.1.0)
14
17
  colored (1.2)
15
18
  diffy (3.0.6)
19
+ i18n (0.6.11)
16
20
  inch (0.5.0)
17
21
  pry
18
22
  sparkr (>= 0.2.0)
@@ -24,6 +28,7 @@ GEM
24
28
  metaclass (~> 0.0.1)
25
29
  mocha-on-bacon (0.2.2)
26
30
  mocha (>= 0.13.0)
31
+ multi_json (1.10.1)
27
32
  pry (0.10.1)
28
33
  coderay (~> 1.1.0)
29
34
  method_source (~> 0.8.1)
@@ -40,6 +45,7 @@ PLATFORMS
40
45
  ruby
41
46
 
42
47
  DEPENDENCIES
48
+ activesupport (>= 3.1)
43
49
  bacon
44
50
  bundler (~> 1.3)
45
51
  claide
@@ -29,6 +29,7 @@ Gem::Specification.new do |spec|
29
29
  spec.add_development_dependency "mocha-on-bacon"
30
30
  spec.add_development_dependency "claide" # Example CLI
31
31
  spec.add_development_dependency "inch"
32
+ spec.add_development_dependency 'activesupport', '>= 3.1'
32
33
 
33
34
  spec.add_runtime_dependency 'colored', '~> 1.2'
34
35
  spec.add_runtime_dependency 'diffy'
@@ -24,12 +24,12 @@ module CLIntegracon
24
24
  # Those are added behind the arguments given on +launch+.
25
25
  attr_accessor :default_args
26
26
 
27
- # @return [Hash<String,String>]
28
- # The replace paths, whose keys are expected to occur in the output,
29
- # which are not printed relative to the project, when the subject will
30
- # be executed. These are e.g. paths were side-effects occur, like manipulation
31
- # of user configurations in dot files or caching-specific directories.
32
- attr_accessor :replace_paths
27
+ # @return [Hash<String|Regexp,String>]
28
+ # The replace patterns, whose keys are expected to occur in the output,
29
+ # which should be redacted, when the subject will be executed. These are
30
+ # e.g. paths were side-effects occur, like manipulation of user configurations
31
+ # in dot files or caching-specific directories or just dates and times.
32
+ attr_accessor :replace_patterns
33
33
 
34
34
  # @return [String]
35
35
  # The path where the output of the executable will be written to.
@@ -53,7 +53,7 @@ module CLIntegracon
53
53
  self.executable = executable || name
54
54
  self.environment_vars = {}
55
55
  self.default_args = []
56
- self.replace_paths = {}
56
+ self.replace_patterns = {}
57
57
  self.output_path = 'execution_output.txt'
58
58
  end
59
59
 
@@ -62,7 +62,20 @@ module CLIntegracon
62
62
 
63
63
  # @!group DSL-like Setter
64
64
 
65
- # Define a path, whose occurrences in the output should been replaced by
65
+ # Define a pattern, whose occurrences in the output should be replaced by a
66
+ # given placeholder.
67
+ #
68
+ # @param [Regexp|String] pattern
69
+ # The pattern
70
+ #
71
+ # @param [String] replacement
72
+ # The replacement
73
+ #
74
+ def replace_pattern(pattern, replacement)
75
+ self.replace_patterns[replacement] = pattern
76
+ end
77
+
78
+ # Define a path, whose occurrences in the output should be replaced by
66
79
  # either its basename or a given placeholder.
67
80
  #
68
81
  # @param [String] path
@@ -73,19 +86,20 @@ module CLIntegracon
73
86
  #
74
87
  def replace_path(path, name=nil)
75
88
  name ||= File.basename path
76
- self.replace_paths[name] = path
89
+ self.replace_pattern path, name
77
90
  end
78
91
 
79
92
  # Define a path in the user directory, whose occurrences in the output
80
- # should been replaced by either its basename or a given placeholder.
93
+ # should be replaced by either its basename or a given placeholder.
81
94
  #
82
95
  # @param [String] path
83
96
  # The path
84
97
  #
85
98
  # @param [String] name
86
- # The name of the path, or the basename of the given path
99
+ # The name of the path, or the given path
87
100
  #
88
101
  def replace_user_path(path, name=nil)
102
+ name ||= "$HOME/#{path}"
89
103
  self.replace_path %r[/Users/.*/#{path.to_s}], name
90
104
  end
91
105
 
@@ -107,24 +121,86 @@ module CLIntegracon
107
121
  # The output, which is emitted while execution.
108
122
  #
109
123
  def launch(head_arguments='', tail_arguments='')
110
- vars = environment_vars.map { |key,value| "#{key}=#{value}" }.join ' '
124
+ command = command_line(head_arguments, tail_arguments)
125
+ output = apply_replacements(run(command))
126
+ write_output(command, output)
127
+ output
128
+ end
129
+
130
+ #-----------------------------------------------------------------------------#
131
+
132
+ # @!group Helpers
133
+
134
+ protected
135
+
136
+ # Serializes configured environment variables
137
+ #
138
+ # @return [String]
139
+ # Serialized assignment of configured environment variables.
140
+ #
141
+ def environment_var_assignments
142
+ environment_vars.map { |key,value| "#{key}=#{value}" }.join ' '
143
+ end
144
+
145
+ # Run the command.
146
+ #
147
+ # @param [String] command_line
148
+ # THe command line to execute
149
+ #
150
+ # @return [String]
151
+ # The output, which is emitted while execution.
152
+ #
153
+ def run(command_line)
154
+ `#{environment_var_assignments} #{command_line} 2>&1`
155
+ end
156
+
157
+ # Merges the given with the configured arguments and returns the command
158
+ # line to execute.
159
+ #
160
+ # @param [String] head_arguments
161
+ # The arguments to pass to the executable before the default arguments.
162
+ #
163
+ # @param [String] tail_arguments
164
+ # The arguments to pass to the executable after the default arguments.
165
+ #
166
+ # @return [String]
167
+ # The command line to execute.
168
+ #
169
+ def command_line(head_arguments='', tail_arguments='')
111
170
  args = [head_arguments, default_args, tail_arguments].flatten.compact.select { |s| s.length > 0 }.join ' '
112
- command = "#{vars} #{executable} #{args} 2>&1"
171
+ "#{executable} #{args}"
172
+ end
113
173
 
114
- output = `#{command}`
174
+ # Apply the configured replacements to the output.
175
+ #
176
+ # @param [String] output
177
+ # The output, which was emitted while execution.
178
+ #
179
+ # @return [String]
180
+ # The redacted output.
181
+ #
182
+ def apply_replacements(output)
183
+ replace_patterns.each do |key, path|
184
+ output = output.gsub(path, key)
185
+ end
186
+ output
187
+ end
115
188
 
189
+ # Saves the output in a file called #output_path, relative to current dir.
190
+ #
191
+ # @param [String] command
192
+ # The executed command line.
193
+ #
194
+ # @param [String] output
195
+ # The output, which was emitted while execution.
196
+ #
197
+ def write_output(command, output)
116
198
  File.open(output_path, 'w') do |file|
117
- file.write command.sub(executable, name)
199
+ printable_command = "#{environment_var_assignments} #{command} 2>&1"
200
+ file.write printable_command.sub(executable, name)
118
201
  file.write "\n"
119
-
120
- replace_paths.each do |key, path|
121
- output.gsub!(path, key)
122
- end
123
-
124
202
  file.write output
125
203
  end
126
-
127
- output
128
204
  end
129
205
 
130
206
  end
@@ -1,3 +1,3 @@
1
1
  module CLIntegracon
2
- VERSION = "0.5.3"
2
+ VERSION = "0.6.0"
3
3
  end
@@ -1,2 +1,3 @@
1
1
  require 'CLIntegracon'
2
2
  require 'mocha-on-bacon'
3
+ require 'active_support/core_ext/string/strip'
@@ -0,0 +1,47 @@
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ describe CLIntegracon::Subject do
4
+
5
+ def subject
6
+ CLIntegracon::Subject
7
+ end
8
+
9
+ before do
10
+ @subject = subject.new('cat')
11
+ end
12
+
13
+ describe '#launch' do
14
+ it 'should redact paths from output' do
15
+ output = <<-eos.strip_heredoc
16
+ /Users/marius/.im/chat.log
17
+ /tmp/im/chat.log
18
+ eos
19
+ @subject.expects(:`).returns(output)
20
+ @subject.stubs(:write_output)
21
+ @subject.replace_user_path '.im/chat.log'
22
+ @subject.replace_path '/tmp', '$TMP'
23
+ @subject.launch.should.be == <<-eos.strip_heredoc
24
+ $HOME/.im/chat.log
25
+ $TMP/im/chat.log
26
+ eos
27
+ end
28
+
29
+ it 'should redact patterns from output' do
30
+ output = <<-eos.strip_heredoc
31
+ Fri Nov 14 22:46:37 - @samuel > ¡Hola!
32
+ Fri Nov 14 22:46:54 - @olivier > Hi
33
+ Fri Nov 14 22:47:13 - @marius > hey
34
+ eos
35
+ @subject.expects(:`).returns(output)
36
+ @subject.stubs(:write_output)
37
+ @subject.replace_pattern /\w{3} \w{3} \d{2} \d{2}:\d{2}:\d{2}/, '<#DATE#>'
38
+ @subject.replace_pattern /@\w+/, '<REDACTED>'
39
+ @subject.launch.should.be == <<-eos.strip_heredoc
40
+ <#DATE#> - <REDACTED> > ¡Hola!
41
+ <#DATE#> - <REDACTED> > Hi
42
+ <#DATE#> - <REDACTED> > hey
43
+ eos
44
+ end
45
+ end
46
+
47
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clintegracon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.3
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marius Rackwitz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-07 00:00:00.000000000 Z
11
+ date: 2014-11-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -108,6 +108,20 @@ dependencies:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: activesupport
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '3.1'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '3.1'
111
125
  - !ruby/object:Gem::Dependency
112
126
  name: colored
113
127
  requirement: !ruby/object:Gem::Requirement
@@ -183,6 +197,7 @@ files:
183
197
  - spec/unit/configuration_spec.rb
184
198
  - spec/unit/formatter_spec.rb
185
199
  - spec/unit/spec_helper.rb
200
+ - spec/unit/subject_spec.rb
186
201
  homepage: https://github.com/mrackwitz/CLIntegracon
187
202
  licenses:
188
203
  - MIT
@@ -229,4 +244,5 @@ test_files:
229
244
  - spec/unit/configuration_spec.rb
230
245
  - spec/unit/formatter_spec.rb
231
246
  - spec/unit/spec_helper.rb
247
+ - spec/unit/subject_spec.rb
232
248
  has_rdoc: