producer-core 0.1.12 → 0.1.13

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.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/Guardfile +4 -3
  3. data/features/actions/file_replace_content.feature +31 -0
  4. data/features/recipes/registry.feature +21 -0
  5. data/features/steps/remote_steps.rb +8 -0
  6. data/features/tasks/registry.feature +13 -0
  7. data/features/tests/file_contains.feature +28 -0
  8. data/lib/producer/core/action.rb +0 -2
  9. data/lib/producer/core/actions/file_replace_content.rb +27 -0
  10. data/lib/producer/core/condition/dsl.rb +4 -3
  11. data/lib/producer/core/env.rb +15 -5
  12. data/lib/producer/core/recipe/dsl.rb +8 -0
  13. data/lib/producer/core/remote/environment.rb +0 -2
  14. data/lib/producer/core/remote/fs.rb +9 -9
  15. data/lib/producer/core/remote.rb +1 -4
  16. data/lib/producer/core/task/dsl.rb +7 -2
  17. data/lib/producer/core/test.rb +0 -2
  18. data/lib/producer/core/testing/mock_remote.rb +25 -0
  19. data/lib/producer/core/testing.rb +1 -0
  20. data/lib/producer/core/tests/file_contains.rb +18 -0
  21. data/lib/producer/core/version.rb +1 -1
  22. data/lib/producer/core.rb +9 -0
  23. data/producer-core.gemspec +2 -2
  24. data/spec/producer/core/action_spec.rb +1 -42
  25. data/spec/producer/core/actions/echo_spec.rb +11 -8
  26. data/spec/producer/core/actions/file_replace_content_spec.rb +49 -0
  27. data/spec/producer/core/actions/file_writer_spec.rb +20 -17
  28. data/spec/producer/core/actions/mkdir_spec.rb +15 -13
  29. data/spec/producer/core/actions/shell_command_spec.rb +16 -14
  30. data/spec/producer/core/condition/dsl_spec.rb +53 -51
  31. data/spec/producer/core/env_spec.rb +30 -2
  32. data/spec/producer/core/prompter_spec.rb +0 -1
  33. data/spec/producer/core/recipe/dsl_spec.rb +82 -66
  34. data/spec/producer/core/remote/environment_spec.rb +32 -30
  35. data/spec/producer/core/remote/fs_spec.rb +100 -104
  36. data/spec/producer/core/remote_spec.rb +4 -13
  37. data/spec/producer/core/task/dsl_spec.rb +82 -72
  38. data/spec/producer/core/task_spec.rb +1 -1
  39. data/spec/producer/core/test_spec.rb +1 -77
  40. data/spec/producer/core/testing/mock_remote_spec.rb +46 -0
  41. data/spec/producer/core/tests/file_contains_spec.rb +46 -0
  42. data/spec/producer/core/tests/has_dir_spec.rb +15 -18
  43. data/spec/producer/core/tests/has_env_spec.rb +34 -34
  44. data/spec/producer/core/tests/has_file_spec.rb +15 -18
  45. data/spec/spec_helper.rb +3 -3
  46. data/spec/support/shared_action.rb +44 -0
  47. data/spec/support/shared_test.rb +82 -0
  48. data/spec/support/test_env_helpers.rb +34 -0
  49. metadata +33 -9
@@ -1,22 +1,24 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  module Producer::Core
4
- describe Actions::ShellCommand do
5
- let(:env) { Env.new(output: StringIO.new) }
6
- let(:command_args) { 'hello from remote host' }
7
- let(:command) { "echo #{command_args}" }
8
- subject(:sh) { Actions::ShellCommand.new(env, command) }
4
+ module Actions
5
+ describe ShellCommand, :env do
6
+ let(:command_args) { 'hello from remote host' }
7
+ let(:command) { "echo #{command_args}" }
8
+ subject(:sh) { ShellCommand.new(env, command) }
9
9
 
10
- describe '#apply' do
11
- it 'executes the remote command' do
12
- expect(sh.remote).to receive(:execute).with(command)
13
- sh.apply
14
- end
10
+ it_behaves_like 'action'
11
+
12
+ describe '#apply' do
13
+ it 'executes the remote command' do
14
+ expect_execution(command)
15
+ sh.apply
16
+ end
15
17
 
16
- it 'writes the returned output with a record separator' do
17
- allow(sh.remote).to receive(:execute) { command_args }
18
- sh.apply
19
- expect(sh.output.string).to eq "#{command_args}\n"
18
+ it 'writes the returned output with a record separator' do
19
+ sh.apply
20
+ expect(output).to eq "#{command_args}\n"
21
+ end
20
22
  end
21
23
  end
22
24
  end
@@ -1,76 +1,78 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  module Producer::Core
4
- describe Condition::DSL do
5
- let(:block) { proc { :some_condition_code } }
6
- let(:env) { double 'env' }
7
- subject(:dsl) { Condition::DSL.new(env, &block) }
4
+ class Condition
5
+ describe DSL do
6
+ let(:block) { proc { :some_condition_code } }
7
+ let(:env) { double 'env' }
8
+ subject(:dsl) { DSL.new(env, &block) }
8
9
 
9
- %w[has_dir has_env has_file].each do |test|
10
- it "has `#{test}' test defined" do
11
- expect(dsl).to respond_to test.to_sym
10
+ %w[file_contains has_dir has_env has_file].each do |test|
11
+ it "has `#{test}' test defined" do
12
+ expect(dsl).to respond_to test.to_sym
13
+ end
12
14
  end
13
- end
14
-
15
- describe '.define_test' do
16
- let(:some_test_class) { Test }
17
15
 
18
- before { Condition::DSL.define_test(:some_test, some_test_class) }
19
-
20
- it 'defines a new test keyword' do
21
- expect(dsl).to respond_to :some_test
22
- end
16
+ describe '.define_test' do
17
+ let(:some_test_class) { Test }
23
18
 
24
- it 'defines the negated test' do
25
- expect(dsl).to respond_to :no_some_test
26
- end
19
+ before { described_class.define_test(:some_test, some_test_class) }
27
20
 
28
- context 'when a test keyword is called' do
29
- it 'registers the test' do
30
- expect { dsl.some_test }.to change { dsl.tests.count }.by 1
21
+ it 'defines a new test keyword' do
22
+ expect(dsl).to respond_to :some_test
31
23
  end
32
24
 
33
- it 'registers the test with current env' do
34
- dsl.some_test
35
- expect(dsl.tests.first.env).to be env
25
+ it 'defines the negated test' do
26
+ expect(dsl).to respond_to :no_some_test
36
27
  end
37
28
 
38
- it 'registers the test with given arguments' do
39
- dsl.some_test :some, :args
40
- expect(dsl.tests.first.arguments).to eq [:some, :args]
29
+ context 'when a test keyword is called' do
30
+ it 'registers the test' do
31
+ expect { dsl.some_test }.to change { dsl.tests.count }.by 1
32
+ end
33
+
34
+ it 'registers the test with current env' do
35
+ dsl.some_test
36
+ expect(dsl.tests.first.env).to be env
37
+ end
38
+
39
+ it 'registers the test with given arguments' do
40
+ dsl.some_test :some, :args
41
+ expect(dsl.tests.first.arguments).to eq [:some, :args]
42
+ end
41
43
  end
42
- end
43
44
 
44
- context 'when a negated test keyword is called' do
45
- it 'registers a negated test' do
46
- dsl.no_some_test
47
- expect(dsl.tests.first).to be_negated
45
+ context 'when a negated test keyword is called' do
46
+ it 'registers a negated test' do
47
+ dsl.no_some_test
48
+ expect(dsl.tests.first).to be_negated
49
+ end
48
50
  end
49
51
  end
50
- end
51
52
 
52
- describe '#initialize' do
53
- it 'assigns the env' do
54
- expect(dsl.env).to be env
55
- end
53
+ describe '#initialize' do
54
+ it 'assigns the env' do
55
+ expect(dsl.env).to be env
56
+ end
56
57
 
57
- it 'assigns the code' do
58
- expect(dsl.block).to be block
59
- end
58
+ it 'assigns the code' do
59
+ expect(dsl.block).to be block
60
+ end
60
61
 
61
- it 'assigns no test' do
62
- expect(dsl.tests).to be_empty
62
+ it 'assigns no test' do
63
+ expect(dsl.tests).to be_empty
64
+ end
63
65
  end
64
- end
65
66
 
66
- describe '#evaluate' do
67
- it 'evaluates its code' do
68
- dsl = described_class.new(env) { throw :condition_code }
69
- expect { dsl.evaluate }.to throw_symbol :condition_code
70
- end
67
+ describe '#evaluate' do
68
+ it 'evaluates its code' do
69
+ dsl = described_class.new(env) { throw :condition_code }
70
+ expect { dsl.evaluate }.to throw_symbol :condition_code
71
+ end
71
72
 
72
- it 'returns the value returned by the assigned block' do
73
- expect(dsl.evaluate).to eq block.call
73
+ it 'returns the value returned by the assigned block' do
74
+ expect(dsl.evaluate).to eq block.call
75
+ end
74
76
  end
75
77
  end
76
78
  end
@@ -17,12 +17,16 @@ module Producer::Core
17
17
  expect(env.target).not_to be
18
18
  end
19
19
 
20
+ it 'assigns an empty registry' do
21
+ expect(env.registry).to be_empty
22
+ end
23
+
20
24
  context 'when input is given as argument' do
21
25
  let(:input) { double 'input' }
22
26
  subject(:env) { described_class.new(input: input) }
23
27
 
24
28
  it 'assigns the given input' do
25
- expect(env.input).to eq input
29
+ expect(env.input).to be input
26
30
  end
27
31
  end
28
32
 
@@ -31,7 +35,16 @@ module Producer::Core
31
35
  subject(:env) { described_class.new(output: output) }
32
36
 
33
37
  it 'assigns the given output' do
34
- expect(env.output).to eq output
38
+ expect(env.output).to be output
39
+ end
40
+ end
41
+
42
+ context 'when remote is given as argument' do
43
+ let(:remote) { double 'remote' }
44
+ subject(:env) { described_class.new(remote: remote) }
45
+
46
+ it 'assigns the given remote' do
47
+ expect(env.remote).to be remote
35
48
  end
36
49
  end
37
50
  end
@@ -62,5 +75,20 @@ module Producer::Core
62
75
  expect(env.remote).to be env.remote
63
76
  end
64
77
  end
78
+
79
+ describe '#[]' do
80
+ subject(:env) { Env.new(registry: { some_key: :some_value }) }
81
+
82
+ it 'returns the value indexed by given key from the registry' do
83
+ expect(env[:some_key]).to eq :some_value
84
+ end
85
+ end
86
+
87
+ describe '#[]=' do
88
+ it 'registers given value at given index in the registry' do
89
+ env[:some_key] = :some_value
90
+ expect(env[:some_key]).to eq :some_value
91
+ end
92
+ end
65
93
  end
66
94
  end
@@ -4,7 +4,6 @@ module Producer::Core
4
4
  describe Prompter do
5
5
  let(:input) { StringIO.new }
6
6
  let(:output) { StringIO.new }
7
- let(:env) { Env.new(input: input, output: output) }
8
7
  subject(:prompter) { Prompter.new(input, output) }
9
8
 
10
9
  describe '#initialize' do
@@ -1,102 +1,118 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  module Producer::Core
4
- describe Recipe::DSL do
5
- include FixturesHelpers
4
+ class Recipe
5
+ describe DSL do
6
+ include FixturesHelpers
6
7
 
7
- let(:code) { proc { :some_recipe_code } }
8
- let(:env) { Env.new }
9
- subject(:dsl) { Recipe::DSL.new(env, &code) }
8
+ let(:code) { proc { :some_recipe_code } }
9
+ let(:env) { Env.new }
10
+ subject(:dsl) { DSL.new(env, &code) }
10
11
 
11
- describe '#initialize' do
12
- it 'assigns the given env' do
13
- expect(dsl.env).to be env
14
- end
12
+ describe '#initialize' do
13
+ it 'assigns the given env' do
14
+ expect(dsl.env).to be env
15
+ end
15
16
 
16
- it 'assigns no task' do
17
- expect(dsl.tasks).to be_empty
18
- end
17
+ it 'assigns no task' do
18
+ expect(dsl.tasks).to be_empty
19
+ end
19
20
 
20
- context 'when a string of code is given as argument' do
21
- let(:code) { 'some_code' }
22
- subject(:dsl) { described_class.new(env, code) }
21
+ context 'when a string of code is given as argument' do
22
+ let(:code) { 'some_code' }
23
+ subject(:dsl) { described_class.new(env, code) }
23
24
 
24
- it 'assigns the string of code' do
25
- expect(dsl.code).to eq code
25
+ it 'assigns the string of code' do
26
+ expect(dsl.code).to eq code
27
+ end
26
28
  end
27
- end
28
29
 
29
- context 'when a code block is given as argument' do
30
- it 'assigns the code block' do
31
- expect(dsl.block).to be code
30
+ context 'when a code block is given as argument' do
31
+ it 'assigns the code block' do
32
+ expect(dsl.block).to be code
33
+ end
32
34
  end
33
35
  end
34
- end
35
36
 
36
- describe '#tasks' do
37
- let(:code) { proc { task(:some_task) {} } }
37
+ describe '#tasks' do
38
+ let(:code) { proc { task(:some_task) {} } }
38
39
 
39
- it 'returns registered tasks' do
40
- dsl.evaluate
41
- expect(dsl.tasks[0].name).to eq :some_task
40
+ it 'returns registered tasks' do
41
+ dsl.evaluate
42
+ expect(dsl.tasks[0].name).to eq :some_task
43
+ end
42
44
  end
43
- end
44
45
 
45
- describe '#evaluate' do
46
- it 'evaluates its code' do
47
- dsl = described_class.new(env) { throw :recipe_code }
48
- expect { dsl.evaluate }.to throw_symbol :recipe_code
49
- end
46
+ describe '#evaluate' do
47
+ it 'evaluates its code' do
48
+ dsl = described_class.new(env) { throw :recipe_code }
49
+ expect { dsl.evaluate }.to throw_symbol :recipe_code
50
+ end
50
51
 
51
- it 'returns itself' do
52
- expect(dsl.evaluate).to eq dsl
52
+ it 'returns itself' do
53
+ expect(dsl.evaluate).to eq dsl
54
+ end
53
55
  end
54
- end
55
56
 
56
- describe '#source' do
57
- let(:filepath) { fixture_path_for 'recipes/throw' }
57
+ describe '#source' do
58
+ let(:filepath) { fixture_path_for 'recipes/throw' }
58
59
 
59
- it 'sources the recipe given as argument' do
60
- expect { dsl.source filepath }.to throw_symbol :recipe_code
60
+ it 'sources the recipe given as argument' do
61
+ expect { dsl.source filepath }.to throw_symbol :recipe_code
62
+ end
61
63
  end
62
- end
63
64
 
64
- describe '#target' do
65
- let(:host) { 'some_host.example' }
65
+ describe '#target' do
66
+ let(:host) { 'some_host.example' }
66
67
 
67
- it 'registers the target host in the env' do
68
- dsl.target host
69
- expect(env.target).to eq host
68
+ it 'registers the target host in the env' do
69
+ dsl.target host
70
+ expect(env.target).to eq host
71
+ end
70
72
  end
71
- end
72
73
 
73
- describe '#task' do
74
- it 'registers a new evaluated task' do
75
- expect { dsl.task(:some_task) { :some_task_code } }
76
- .to change { dsl.tasks.count }.by 1
74
+ describe '#task' do
75
+ it 'registers a new evaluated task' do
76
+ expect { dsl.task(:some_task) { :some_task_code } }
77
+ .to change { dsl.tasks.count }.by 1
78
+ end
77
79
  end
78
- end
79
80
 
80
- describe '#macro' do
81
- it 'defines the new recipe keyword' do
82
- dsl.macro :hello
83
- expect(dsl).to respond_to(:hello)
84
- end
81
+ describe '#macro' do
82
+ it 'defines the new recipe keyword' do
83
+ dsl.macro :hello
84
+ expect(dsl).to respond_to(:hello)
85
+ end
86
+
87
+ context 'when a defined macro is called' do
88
+ before { dsl.macro(:hello) { :some_macro_code } }
89
+
90
+ it 'registers the new task' do
91
+ expect { dsl.hello }.to change { dsl.tasks.count }.by 1
92
+ end
93
+ end
85
94
 
86
- context 'when a defined macro is called' do
87
- before { dsl.macro(:hello) { :some_macro_code } }
95
+ context 'when a defined macro is called with arguments' do
96
+ before { dsl.macro(:hello) { |a, b| echo a, b } }
88
97
 
89
- it 'registers the new task' do
90
- expect { dsl.hello }.to change { dsl.tasks.count }.by 1
98
+ it 'evaluates task code with arguments' do
99
+ dsl.hello :some, :args
100
+ expect(dsl.tasks.first.actions.first.arguments).to eq [:some, :args]
101
+ end
91
102
  end
92
103
  end
93
104
 
94
- context 'when a defined macro is called with arguments' do
95
- before { dsl.macro(:hello) { |a, b| echo a, b } }
105
+ describe '#set' do
106
+ it 'registers a key/value pair in env registry' do
107
+ dsl.set :some_key, :some_value
108
+ expect(env[:some_key]).to eq :some_value
109
+ end
110
+ end
96
111
 
97
- it 'evaluates task code with arguments' do
98
- dsl.hello :some, :args
99
- expect(dsl.tasks.first.actions.first.arguments).to eq [:some, :args]
112
+ describe '#get' do
113
+ it 'fetches a value from the registry at given index' do
114
+ dsl.set :some_key, :some_value
115
+ expect(dsl.get :some_key).to eq :some_value
100
116
  end
101
117
  end
102
118
  end
@@ -1,48 +1,50 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  module Producer::Core
4
- describe Remote::Environment do
5
- let(:variables) { { 'FOO' => 'bar', 'BAZ' => 'qux' } }
6
- let(:string) { "FOO=bar\nBAZ=qux" }
7
- let(:argument) { variables }
8
- subject(:environment) { Remote::Environment.new(argument) }
4
+ class Remote
5
+ describe Environment do
6
+ let(:variables) { { 'FOO' => 'bar', 'BAZ' => 'qux' } }
7
+ let(:string) { "FOO=bar\nBAZ=qux" }
8
+ let(:argument) { variables }
9
+ subject(:environment) { Environment.new(argument) }
9
10
 
10
- describe '.string_to_hash' do
11
- it 'converts key=value pairs separated by new lines to a hash' do
12
- expect(described_class.string_to_hash(string)).to eq variables
11
+ describe '.string_to_hash' do
12
+ it 'converts key=value pairs separated by new lines to a hash' do
13
+ expect(described_class.string_to_hash(string)).to eq variables
14
+ end
13
15
  end
14
- end
15
16
 
16
- describe '.new_from_string' do
17
- it 'returns a new instance with converted keys and values' do
18
- environment = described_class.new_from_string string
19
- expect(environment.variables).to eq variables
17
+ describe '.new_from_string' do
18
+ it 'returns a new instance with converted keys and values' do
19
+ environment = described_class.new_from_string string
20
+ expect(environment.variables).to eq variables
21
+ end
20
22
  end
21
- end
22
23
 
23
- describe '#initialize' do
24
- it 'assigns the key/value pairs' do
25
- expect(environment.variables).to eq variables
24
+ describe '#initialize' do
25
+ it 'assigns the key/value pairs' do
26
+ expect(environment.variables).to eq variables
27
+ end
26
28
  end
27
- end
28
29
 
29
- describe '#key?' do
30
- context 'when key is defined' do
31
- it 'returns true' do
32
- expect(environment.key? 'FOO').to be true
30
+ describe '#key?' do
31
+ context 'when key is defined' do
32
+ it 'returns true' do
33
+ expect(environment.key? 'FOO').to be true
34
+ end
33
35
  end
34
- end
35
36
 
36
- context 'when key is not defined' do
37
- it 'returns false' do
38
- expect(environment.key? 'INEXISTENT_KEY').to be false
37
+ context 'when key is not defined' do
38
+ it 'returns false' do
39
+ expect(environment.key? 'INEXISTENT_KEY').to be false
40
+ end
39
41
  end
40
42
  end
41
- end
42
43
 
43
- describe '#[]' do
44
- it 'returns the value indexed by given key' do
45
- expect(environment['FOO']).to eq 'bar'
44
+ describe '#[]' do
45
+ it 'returns the value indexed by given key' do
46
+ expect(environment['FOO']).to eq 'bar'
47
+ end
46
48
  end
47
49
  end
48
50
  end