singularity_dsl 1.2.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +17 -0
  3. data/.rspec +2 -0
  4. data/.rubocop.yml +10 -0
  5. data/.singularityrc +12 -0
  6. data/Gemfile +6 -0
  7. data/Gemfile.lock +52 -0
  8. data/LICENSE.txt +22 -0
  9. data/README.md +38 -0
  10. data/Rakefile +24 -0
  11. data/bin/singularity_runner +5 -0
  12. data/lib/singularity_dsl/application.rb +76 -0
  13. data/lib/singularity_dsl/cli/cli.rb +125 -0
  14. data/lib/singularity_dsl/cli/table.rb +32 -0
  15. data/lib/singularity_dsl/dsl/batch.rb +23 -0
  16. data/lib/singularity_dsl/dsl/changeset.rb +39 -0
  17. data/lib/singularity_dsl/dsl/components.rb +7 -0
  18. data/lib/singularity_dsl/dsl/dsl.rb +71 -0
  19. data/lib/singularity_dsl/dsl/event_store.rb +34 -0
  20. data/lib/singularity_dsl/dsl/registry.rb +60 -0
  21. data/lib/singularity_dsl/dsl/runner.rb +67 -0
  22. data/lib/singularity_dsl/dsl/utils.rb +28 -0
  23. data/lib/singularity_dsl/errors.rb +35 -0
  24. data/lib/singularity_dsl/files.rb +20 -0
  25. data/lib/singularity_dsl/git_helper.rb +94 -0
  26. data/lib/singularity_dsl/runstate.rb +30 -0
  27. data/lib/singularity_dsl/stdout.rb +16 -0
  28. data/lib/singularity_dsl/task.rb +30 -0
  29. data/lib/singularity_dsl/tasks/rake.rb +31 -0
  30. data/lib/singularity_dsl/tasks/rspec.rb +32 -0
  31. data/lib/singularity_dsl/tasks/rubocop.rb +54 -0
  32. data/lib/singularity_dsl/tasks/shell_task.rb +90 -0
  33. data/lib/singularity_dsl/version.rb +6 -0
  34. data/lib/singularity_dsl.rb +9 -0
  35. data/singularity_dsl.gemspec +31 -0
  36. data/spec/singularity_dsl/application_spec.rb +92 -0
  37. data/spec/singularity_dsl/dsl/batch_spec.rb +30 -0
  38. data/spec/singularity_dsl/dsl/changeset_spec.rb +54 -0
  39. data/spec/singularity_dsl/dsl/dsl_spec.rb +45 -0
  40. data/spec/singularity_dsl/dsl/event_store_spec.rb +44 -0
  41. data/spec/singularity_dsl/dsl/registry_spec.rb +39 -0
  42. data/spec/singularity_dsl/dsl/runner_spec.rb +39 -0
  43. data/spec/singularity_dsl/dsl/stubs/tasks/dummy_task.rb +4 -0
  44. data/spec/singularity_dsl/dsl/utils_spec.rb +33 -0
  45. data/spec/singularity_dsl/git_helper_spec.rb +72 -0
  46. data/spec/singularity_dsl/runstate_spec.rb +73 -0
  47. data/spec/singularity_dsl/task_spec.rb +54 -0
  48. data/spec/singularity_dsl/tasks/shell_task_spec.rb +119 -0
  49. metadata +231 -0
@@ -0,0 +1,30 @@
1
+ # encoding: utf-8
2
+
3
+ require 'singularity_dsl/dsl/batch'
4
+
5
+ # dummy class to use for block contexts
6
+ class TestObject
7
+ def meth
8
+ end
9
+ end
10
+
11
+ describe 'Batch' do
12
+ context '#initialize' do
13
+ it 'converts name to symbol' do
14
+ batch = SingularityDsl::Dsl::Batch.new('test', self)
15
+ expect(batch.name).to eql(:test)
16
+ end
17
+ end
18
+
19
+ context '#execute' do
20
+ it 'executes block in passed context' do
21
+ dummy = TestObject.new
22
+ dummy.stub(:meth)
23
+ batch = SingularityDsl::Dsl::Batch.new('test', dummy) do |thing|
24
+ thing.meth
25
+ end
26
+ expect(dummy).to receive(:meth)
27
+ batch.execute
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,54 @@
1
+ # encoding: utf-8
2
+
3
+ require 'singularity_dsl/dsl/changeset'
4
+
5
+ # container to include DslChangeset
6
+ class ChangesetTest
7
+ include SingularityDsl::Dsl::Changeset
8
+ end
9
+
10
+ describe 'DslChangeset' do
11
+ let(:instance) { ChangesetTest.new }
12
+ before :each do
13
+ instance.changeset = %w(something.php something.js something.css)
14
+ end
15
+
16
+ context '#files_changed?' do
17
+ it 'correct eval for single file type' do
18
+ expect(instance.files_changed? 'php').to eql true
19
+ end
20
+
21
+ it 'correct eval for multiple file types' do
22
+ expect(instance.files_changed? %w(js css)).to eql true
23
+ end
24
+ end
25
+
26
+ context '#changed_files' do
27
+ before :each do
28
+ ::File.stub(:exist?)
29
+ .with('something.css')
30
+ .and_return(true)
31
+ ::File.stub(:exist?)
32
+ .with('something.js')
33
+ .and_return(true)
34
+ ::File.stub(:exist?)
35
+ .with('something.php')
36
+ .and_return(false)
37
+ end
38
+
39
+ it 'correct eval for single file type' do
40
+ expect(instance.changed_files 'css').to eql %w(something.css)
41
+ end
42
+
43
+ it 'correct eval for multiple file types' do
44
+ expect(instance.changed_files %w(js css))
45
+ .to eql %w(something.css something.js)
46
+ end
47
+
48
+ it 'filters for existing files' do
49
+ expect(instance.changed_files %w(php js css))
50
+ .to eql %w(something.css something.js)
51
+ expect(instance.changed_files 'php').to eql []
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+
3
+ require 'singularity_dsl/dsl/dsl'
4
+ require 'singularity_dsl/dsl/registry'
5
+ require 'singularity_dsl/task'
6
+
7
+ class TestTask < SingularityDsl::Task
8
+ end
9
+
10
+ describe 'Dsl' do
11
+ let(:dsl) { SingularityDsl::Dsl::Dsl.new }
12
+
13
+ context '#initialize' do
14
+ it 'creates registry' do
15
+ expect(dsl.registry).to be_a_kind_of SingularityDsl::Dsl::Registry
16
+ end
17
+ end
18
+
19
+ context '#define_task' do
20
+ it 'creates task function for given task' do
21
+ dsl.define_task TestTask
22
+ expect(dsl.singleton_methods).to include :testtask
23
+ end
24
+
25
+ it 'throws when tasks have the same name' do
26
+ dsl.define_task TestTask
27
+ expect { dsl.define_task TestTask }
28
+ .to raise_error RuntimeError, /task name clash/
29
+ end
30
+ end
31
+
32
+ context '#load_tasks_in_path' do
33
+ it 'does not load tasks that have already been required' do
34
+ path = ::File.dirname(__FILE__) + '/../../lib/singularity_dsl/tasks'
35
+ expect(dsl).to receive(:load_tasks).with []
36
+ dsl.load_tasks_in_path path
37
+ end
38
+
39
+ it 'actually loads new tasks from dir' do
40
+ path = ::File.dirname(__FILE__) + '/stubs/tasks'
41
+ dsl.load_tasks_in_path path
42
+ expect(dsl.singleton_methods).to include :dummytask
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,44 @@
1
+ # encoding: utf-8
2
+
3
+ require 'singularity_dsl/dsl/event_store'
4
+
5
+ # container to include DslEventStore
6
+ class EventStoreTest
7
+ include SingularityDsl::Dsl::EventStore
8
+ end
9
+
10
+ describe 'DslEventStore' do
11
+ let(:instance) { EventStoreTest.new }
12
+
13
+ context '#on_error' do
14
+ it 'creates an error_proc' do
15
+ expect(instance.error_proc).to be_a_kind_of Proc
16
+ instance.on_error {}
17
+ expect(instance.error_proc).to be_a_kind_of Proc
18
+ end
19
+ end
20
+
21
+ context '#on_fail' do
22
+ it 'creates an fail_proc' do
23
+ expect(instance.fail_proc).to be_a_kind_of Proc
24
+ instance.on_fail {}
25
+ expect(instance.fail_proc).to be_a_kind_of Proc
26
+ end
27
+ end
28
+
29
+ context '#on_success' do
30
+ it 'creates an error_proc' do
31
+ expect(instance.success_proc).to be_a_kind_of Proc
32
+ instance.on_success {}
33
+ expect(instance.success_proc).to be_a_kind_of Proc
34
+ end
35
+ end
36
+
37
+ context '#always' do
38
+ it 'creates an error_proc' do
39
+ expect(instance.always_proc).to be_a_kind_of Proc
40
+ instance.always {}
41
+ expect(instance.always_proc).to be_a_kind_of Proc
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,39 @@
1
+ # encoding: utf-8
2
+
3
+ require 'singularity_dsl/dsl/registry'
4
+ require 'singularity_dsl/task'
5
+
6
+ class TestTask < SingularityDsl::Task
7
+ end
8
+
9
+ describe 'DslRegistry' do
10
+ before :each do
11
+ @instance = SingularityDsl::Dsl::Registry.new
12
+ end
13
+
14
+ context '#initialize' do
15
+ it 'creates empty task_list array' do
16
+ expect(@instance.run_list).to be_kind_of Array
17
+ expect(@instance.run_list).to be_empty
18
+ end
19
+ end
20
+
21
+ context '#add_task' do
22
+ it 'adds tasks' do
23
+ task = TestTask.new
24
+ @instance.add_task task
25
+ expect(@instance.run_list).to_not be_empty
26
+ expect(@instance.run_list).to eql [task]
27
+ end
28
+
29
+ it 'fails when non-task is given' do
30
+ expect { @instance.add_task 'fail' }
31
+ .to raise_error ::RuntimeError, /Non-Task given - /
32
+ end
33
+
34
+ it 'fails when raw task is given' do
35
+ expect { @instance.add_task SingularityDsl::Task.new }
36
+ .to raise_error ::RuntimeError, /Cannot use raw Task objects - /
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,39 @@
1
+ # encoding: utf-8
2
+
3
+ require 'singularity_dsl/dsl/dsl'
4
+ require 'singularity_dsl/dsl/runner'
5
+ require 'singularity_dsl/runstate'
6
+ require 'singularity_dsl/task'
7
+
8
+ describe 'DslRunner' do
9
+ let(:runner) { SingularityDsl::Dsl::Runner.new }
10
+
11
+ context '#initialize' do
12
+ it 'creates base state' do
13
+ expect(runner.state).to be_kind_of SingularityDsl::Runstate
14
+ end
15
+
16
+ it 'creates base DSL' do
17
+ expect(runner.dsl).to be_kind_of SingularityDsl::Dsl::Dsl
18
+ end
19
+ end
20
+
21
+ context '#execute' do
22
+ it 'taps & evaluates failed task.execute statuses correctly' do
23
+ task = SingularityDsl::Task.new
24
+ task.stub(:execute).and_return true
25
+ runner.dsl.registry.stub(:run_list).and_return [task]
26
+ expect(runner).to receive(:record_failure).with task
27
+ expect(runner).to receive(:resource_fail).with task
28
+ runner.execute
29
+ end
30
+ end
31
+
32
+ context '#load_ex_script' do
33
+ it 'instance_evals contents of a file' do
34
+ ::File.stub(:read).and_return('0')
35
+ expect(runner.dsl).to receive(:instance_eval).with('0')
36
+ runner.load_ex_script 'foo'
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,4 @@
1
+ # encoding: utf-8
2
+
3
+ class DummyTask < Task
4
+ end
@@ -0,0 +1,33 @@
1
+ # encoding: utf-8
2
+
3
+ require 'singularity_dsl/dsl/utils'
4
+
5
+ class TestTask < SingularityDsl::Task
6
+ end
7
+
8
+ describe 'Utils' do
9
+ include SingularityDsl::Dsl::Utils
10
+
11
+ context '#task_name' do
12
+ it 'simplifies class names correctly' do
13
+ expect(task_name 'Foo::Bar::Blah').to eql 'Blah'
14
+ end
15
+ end
16
+
17
+ context '#task' do
18
+ it 'returns lowercase sym representing DSL fx' do
19
+ expect(task TestTask).to eql :testtask
20
+ end
21
+ end
22
+
23
+ context '#task_list' do
24
+ it 'returns array of tasks' do
25
+ tasks = task_list
26
+ expect(tasks).to be_a_kind_of Array
27
+ expect(tasks).to_not be_empty
28
+ tasks.each do |task|
29
+ expect(task <= SingularityDsl::Task).to eql true
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,72 @@
1
+ # encoding: utf-8
2
+
3
+ require 'singularity_dsl/git_helper'
4
+
5
+ describe 'GitHelper' do
6
+ before :each do
7
+ SingularityDsl::GitHelper
8
+ .any_instance
9
+ .stub(:git_installed)
10
+ .and_return true
11
+ end
12
+ let(:git) { SingularityDsl::GitHelper.new }
13
+
14
+ context '#initialize' do
15
+ it 'throws if git is not installed' do
16
+ SingularityDsl::GitHelper
17
+ .any_instance
18
+ .stub(:git_installed)
19
+ .and_return false
20
+ expect { SingularityDsl::GitHelper.new }
21
+ .to(raise_error ArgumentError, /git not installed/)
22
+ end
23
+ end
24
+
25
+ context '#clean_reset' do
26
+ it 'fails when reset fails' do
27
+ git.stub(:reset).and_return 1
28
+ git.stub(:clean).and_return 0
29
+ expect { git.clean_reset }
30
+ .to(raise_error RuntimeError, /failed to clean/)
31
+ end
32
+
33
+ it 'fails when clean fails' do
34
+ git.stub(:reset).and_return 0
35
+ git.stub(:clean).and_return 1
36
+ expect { git.clean_reset }
37
+ .to(raise_error RuntimeError, /failed to clean/)
38
+ end
39
+
40
+ it 'fails when both reset & clean fail' do
41
+ git.stub(:reset).and_return 1
42
+ git.stub(:clean).and_return 1
43
+ expect { git.clean_reset }
44
+ .to(raise_error RuntimeError, /failed to clean/)
45
+ end
46
+ end
47
+
48
+ context '#merge_remote' do
49
+ it 'generates & calls correct cmd' do
50
+ git.stub(:remotes).and_return []
51
+ git.stub(:exec).with('git fetch --all')
52
+ git.stub(:exec).with('git merge bar/foo').and_return 0
53
+ git.merge_remote 'foo', 'bar'
54
+ end
55
+ end
56
+
57
+ context '#diff_remote' do
58
+ it 'generates & calls correct cmd w/ single flag' do
59
+ git.stub(:remotes).and_return []
60
+ git.stub(:exec).with('git fetch --all')
61
+ git.stub(:exec).with('git diff bar/foo --flag').and_return 0
62
+ git.diff_remote 'foo', 'bar', '--flag'
63
+ end
64
+
65
+ it 'generates & calls correct cmd w/ multiple flags' do
66
+ git.stub(:remotes).and_return []
67
+ git.stub(:exec).with('git fetch --all')
68
+ git.stub(:exec).with('git diff bar/foo --flag --other').and_return 0
69
+ git.diff_remote 'foo', 'bar', %w(--flag, --other)
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,73 @@
1
+ # encoding: utf-8
2
+
3
+ require 'singularity_dsl/runstate'
4
+
5
+ describe 'Runstate' do
6
+ before :each do
7
+ @instance = SingularityDsl::Runstate.new
8
+ end
9
+
10
+ context '#initialize' do
11
+ it 'sets error & failed to false' do
12
+ expect(@instance.error).to eql false
13
+ expect(@instance.failed).to eql false
14
+ end
15
+
16
+ it 'starts of with empty failure & error arrays' do
17
+ expect(@instance.errors).to be_kind_of Array
18
+ expect(@instance.errors).to be_empty
19
+ expect(@instance.failures).to be_kind_of Array
20
+ expect(@instance.failures).to be_empty
21
+ end
22
+ end
23
+
24
+ context '#add_failure' do
25
+ it 'sets failed to true' do
26
+ @instance.add_failure 'failed'
27
+ expect(@instance.failed).to eql true
28
+ end
29
+
30
+ it 'adds failure' do
31
+ @instance.add_failure 'failed'
32
+ expect(@instance.failures).to eql ['failed']
33
+ end
34
+
35
+ it 'does NOT affect error state' do
36
+ @instance.add_failure 'failed'
37
+ expect(@instance.error).to eql false
38
+ end
39
+ end
40
+
41
+ context '#add_error' do
42
+ it 'sets error to true' do
43
+ @instance.add_error 'error'
44
+ expect(@instance.error).to eql true
45
+ end
46
+
47
+ it 'adds error' do
48
+ @instance.add_error 'error'
49
+ expect(@instance.errors).to eql ['error']
50
+ end
51
+
52
+ it 'does NOT affect failed state' do
53
+ @instance.add_error 'error'
54
+ expect(@instance.failed).to eql false
55
+ end
56
+ end
57
+
58
+ context '#exit_code' do
59
+ it 'returns 1 on error' do
60
+ @instance.add_error 'error'
61
+ expect(@instance.exit_code).to eql 1
62
+ end
63
+
64
+ it 'returns 1 on failure' do
65
+ @instance.add_failure 'failure'
66
+ expect(@instance.exit_code).to eql 1
67
+ end
68
+
69
+ it 'returns 0 otherwise' do
70
+ expect(@instance.exit_code).to eql 0
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,54 @@
1
+ # encoding: utf-8
2
+
3
+ require 'singularity_dsl/task'
4
+
5
+ describe 'Task' do
6
+ context '#initialize' do
7
+ it 'does nothing if no block given' do
8
+ expect(SingularityDsl::Task).to_not receive :instance_eval
9
+ SingularityDsl::Task.new
10
+ end
11
+
12
+ it 'evals given block if given' do
13
+ expect(Kernel).to receive(:puts).with('woooooo')
14
+ SingularityDsl::Task.new { Kernel.puts 'woooooo' }
15
+ end
16
+ end
17
+
18
+ context '#validate_file' do
19
+ it 'throws if file DNE' do
20
+ expect { SingularityDsl::Task.new.validate_file('asdbfadf') }
21
+ .to raise_error(ArgumentError, /Cannot find/)
22
+ end
23
+ end
24
+
25
+ context '#task_name' do
26
+ it 'returns false' do
27
+ expect(SingularityDsl::Task.new.task_name).to eql false
28
+ end
29
+ end
30
+
31
+ context '#execute' do
32
+ it 'throws' do
33
+ expect { SingularityDsl::Task.new.execute }
34
+ .to raise_error(RuntimeError,
35
+ 'SingularityDsl::Task::execute not implemented')
36
+ end
37
+ end
38
+
39
+ context '#failed_status' do
40
+ it 'returns false for specific values' do
41
+ task = SingularityDsl::Task.new
42
+ expect(task.failed_status nil).to eql false
43
+ expect(task.failed_status 0).to eql false
44
+ expect(task.failed_status false).to eql false
45
+ end
46
+ end
47
+
48
+ context '#self.description' do
49
+ it 'auto-generates task description' do
50
+ expect(SingularityDsl::Task.new.description)
51
+ .to eql 'Runs SingularityDsl::Task task'
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,119 @@
1
+ # encoding: utf-8
2
+
3
+ require 'singularity_dsl/tasks/shell_task'
4
+
5
+ describe 'ShellTask' do
6
+ let(:sh_task) { ShellTask.new }
7
+
8
+ context '#initialize' do
9
+ it 'starts with no conditionals' do
10
+ expect(sh_task.conditionals).to eql []
11
+ end
12
+
13
+ it 'has an echo notification as a default alt cmd' do
14
+ expect(sh_task.alternative)
15
+ .to eql 'echo "no alternative shell cmd defined"'
16
+ end
17
+ end
18
+
19
+ context '#condition' do
20
+ it 'throws when non string given' do
21
+ expect { sh_task.condition([]) }.to raise_error
22
+ end
23
+
24
+ it 'appends the cmd to conditionals array' do
25
+ sh_task.condition 'woo'
26
+ expect(sh_task.conditionals).to eql ['woo']
27
+ end
28
+ end
29
+
30
+ context '#no_fail' do
31
+ it 'fails when non-bool given' do
32
+ expect { sh_task.no_fail [] }.to raise_error
33
+ end
34
+ end
35
+
36
+ context '#alt' do
37
+ it 'throws when non string given' do
38
+ expect { sh_task.alt([]) }.to raise_error
39
+ end
40
+
41
+ it 'sets alternative cmd' do
42
+ sh_task.alt 'woo'
43
+ expect(sh_task.alternative).to eql 'woo'
44
+ end
45
+ end
46
+
47
+ context '#command' do
48
+ it 'sets commands!' do
49
+ cmd = 'echo "hi :)"'
50
+ sh_task.command cmd
51
+ expect(sh_task.shell.command).to eql cmd
52
+ end
53
+
54
+ it 'sets live_stream to STDOUT' do
55
+ cmd = 'echo "hi :)"'
56
+ sh_task.command cmd
57
+ expect(sh_task.shell.live_stream).to eql STDOUT
58
+ end
59
+ end
60
+
61
+ context '#task_name' do
62
+ it 'returns command' do
63
+ expect(sh_task.task_name).to eql false
64
+ end
65
+
66
+ it 'returns command' do
67
+ cmd = 'echo "hi :)"'
68
+ sh_task.command cmd
69
+ expect(sh_task.task_name).to eql 'echo "hi :)"'
70
+ end
71
+ end
72
+
73
+ context '#execute' do
74
+ it 'errors when command() was never called' do
75
+ expect { sh_task.execute }.to raise_error
76
+ end
77
+
78
+ it 'runs command returns correct status, with conditionals' do
79
+ cmd = 'echo "hi :)"'
80
+ alt = 'echo "no"'
81
+ sh_task.live_stream = false
82
+ sh_task.command cmd
83
+ sh_task.alt alt
84
+ sh_task.condition 'ls'
85
+ expect(sh_task).to_not receive(:command).with(alt)
86
+ expect(sh_task.shell).to receive(:run_command)
87
+ expect(sh_task.shell).to receive(:exitstatus).and_return 0
88
+ expect(sh_task.execute).to eql 0
89
+ end
90
+
91
+ it 'runs alternative command' do
92
+ cmd = 'echo "hi :)"'
93
+ alt = 'echo "no"'
94
+ sh_task.command cmd
95
+ sh_task.alt alt
96
+ sh_task.condition 'ls -z'
97
+ expect(sh_task).to_not receive(:command).with(cmd)
98
+ expect(sh_task).to receive(:command).with(alt)
99
+ expect(sh_task.shell).to receive(:run_command)
100
+ expect(sh_task.shell).to receive(:exitstatus).and_return 0
101
+ expect(sh_task.execute).to eql 0
102
+ end
103
+
104
+ it 'runs command returns correct status, no conditionals' do
105
+ sh_task.command 'echo "hi :)"'
106
+ expect(sh_task.shell).to receive(:run_command)
107
+ expect(sh_task.shell).to receive(:exitstatus).and_return 0
108
+ expect(sh_task.execute).to eql 0
109
+ end
110
+
111
+ it 'returns 0 if no_fail set' do
112
+ sh_task.command 'ls -z'
113
+ sh_task.no_fail true
114
+ expect(sh_task.shell).to receive(:run_command)
115
+ expect(sh_task.shell).to_not receive(:exitstatus)
116
+ expect(sh_task.execute).to eql 0
117
+ end
118
+ end
119
+ end