omnitest-psychic 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +4 -0
  3. data/.rspec +2 -0
  4. data/.rubocop.yml +5 -0
  5. data/.rubocop_todo.yml +36 -0
  6. data/.travis.yml +10 -0
  7. data/CONTRIBUTING.md +61 -0
  8. data/Gemfile +23 -0
  9. data/LICENSE.txt +22 -0
  10. data/README.md +234 -0
  11. data/Rakefile +12 -0
  12. data/appveyor.yml +9 -0
  13. data/bin/psychic +4 -0
  14. data/docs/code_samples.md +92 -0
  15. data/docs/index.md +128 -0
  16. data/lib/omnitest/output_helper.rb +84 -0
  17. data/lib/omnitest/psychic.rb +191 -0
  18. data/lib/omnitest/psychic/cli.rb +171 -0
  19. data/lib/omnitest/psychic/code2doc.rb +75 -0
  20. data/lib/omnitest/psychic/code2doc/code_helper.rb +122 -0
  21. data/lib/omnitest/psychic/code2doc/code_segmenter.rb +170 -0
  22. data/lib/omnitest/psychic/code2doc/comment_styles.rb +92 -0
  23. data/lib/omnitest/psychic/command_template.rb +35 -0
  24. data/lib/omnitest/psychic/error.rb +15 -0
  25. data/lib/omnitest/psychic/execution/default_strategy.rb +82 -0
  26. data/lib/omnitest/psychic/execution/env_strategy.rb +12 -0
  27. data/lib/omnitest/psychic/execution/flag_strategy.rb +14 -0
  28. data/lib/omnitest/psychic/execution/token_strategy.rb +68 -0
  29. data/lib/omnitest/psychic/factories/go_factories.rb +14 -0
  30. data/lib/omnitest/psychic/factories/hot_read_task_factory.rb +54 -0
  31. data/lib/omnitest/psychic/factories/java_factories.rb +81 -0
  32. data/lib/omnitest/psychic/factories/powershell_factories.rb +53 -0
  33. data/lib/omnitest/psychic/factories/ruby_factories.rb +56 -0
  34. data/lib/omnitest/psychic/factories/shell_factories.rb +89 -0
  35. data/lib/omnitest/psychic/factories/travis_factories.rb +33 -0
  36. data/lib/omnitest/psychic/factory_manager.rb +46 -0
  37. data/lib/omnitest/psychic/file_finder.rb +69 -0
  38. data/lib/omnitest/psychic/hints.rb +21 -0
  39. data/lib/omnitest/psychic/magic_task_factory.rb +98 -0
  40. data/lib/omnitest/psychic/script.rb +146 -0
  41. data/lib/omnitest/psychic/script_factory.rb +66 -0
  42. data/lib/omnitest/psychic/script_factory_manager.rb +24 -0
  43. data/lib/omnitest/psychic/script_runner.rb +45 -0
  44. data/lib/omnitest/psychic/task.rb +6 -0
  45. data/lib/omnitest/psychic/task_factory_manager.rb +19 -0
  46. data/lib/omnitest/psychic/task_runner.rb +30 -0
  47. data/lib/omnitest/psychic/tokens.rb +51 -0
  48. data/lib/omnitest/psychic/version.rb +5 -0
  49. data/lib/omnitest/psychic/workflow.rb +27 -0
  50. data/lib/omnitest/shell.rb +27 -0
  51. data/lib/omnitest/shell/buff_shellout_executor.rb +41 -0
  52. data/lib/omnitest/shell/execution_result.rb +61 -0
  53. data/lib/omnitest/shell/mixlib_shellout_executor.rb +66 -0
  54. data/mkdocs.yml +6 -0
  55. data/omnitest-psychic.gemspec +36 -0
  56. data/spec/fabricators/shell_fabricator.rb +9 -0
  57. data/spec/omnitest/psychic/code2doc/code_helper_spec.rb +123 -0
  58. data/spec/omnitest/psychic/execution/default_strategy_spec.rb +17 -0
  59. data/spec/omnitest/psychic/execution/env_strategy_spec.rb +17 -0
  60. data/spec/omnitest/psychic/execution/flag_strategy_spec.rb +26 -0
  61. data/spec/omnitest/psychic/execution/token_strategy_spec.rb +26 -0
  62. data/spec/omnitest/psychic/factories/java_factories_spec.rb +35 -0
  63. data/spec/omnitest/psychic/factories/powershell_factories_spec.rb +59 -0
  64. data/spec/omnitest/psychic/factories/ruby_factories_spec.rb +91 -0
  65. data/spec/omnitest/psychic/factories/shell_factories_spec.rb +79 -0
  66. data/spec/omnitest/psychic/factories/travis_factories_spec.rb +78 -0
  67. data/spec/omnitest/psychic/hot_read_task_factory_spec.rb +51 -0
  68. data/spec/omnitest/psychic/script_factory_manager_spec.rb +57 -0
  69. data/spec/omnitest/psychic/script_spec.rb +55 -0
  70. data/spec/omnitest/psychic/shell_spec.rb +68 -0
  71. data/spec/omnitest/psychic/workflow_spec.rb +46 -0
  72. data/spec/omnitest/psychic_spec.rb +170 -0
  73. data/spec/spec_helper.rb +52 -0
  74. metadata +352 -0
@@ -0,0 +1,59 @@
1
+ module Omnitest
2
+ class Psychic
3
+ module Factories
4
+ RSpec.describe PowerShellTaskFactory do
5
+ let(:psychic) { Psychic.new(cwd: current_dir) }
6
+ let(:shell) { Omnitest::Shell.shell = double('shell') }
7
+ subject { described_class.new(psychic, cwd: current_dir) }
8
+
9
+ shared_context 'with scripts/*.ps1 files' do
10
+ before(:each) do
11
+ write_file 'scripts/bootstrap.ps1', ''
12
+ write_file 'scripts/compile.ps1', ''
13
+ write_file 'scripts/foo.sh', ''
14
+ end
15
+ end
16
+
17
+ describe 'known_task?' do
18
+ shared_examples 'detects matching scripts' do
19
+ it 'returns true if a matching script exists' do
20
+ expect(subject.known_task? :bootstrap).to be true
21
+ expect(subject.known_task? :compile).to be true
22
+ end
23
+ it 'returns false if a matching script does not exists' do
24
+ expect(subject.known_task? :foo).to be false
25
+ expect(subject.known_task? :bar).to be false
26
+ end
27
+ end
28
+
29
+ context 'with scripts/*.ps1 files' do
30
+ include_context 'with scripts/*.ps1 files' do
31
+ include_examples 'detects matching scripts'
32
+ end
33
+ end
34
+ end
35
+
36
+ describe '#task' do
37
+ context 'matching a task' do
38
+ context 'with scripts/*.ps1 files' do
39
+ include_context 'with scripts/*.ps1 files' do
40
+ it 'returns the script command' do
41
+ expect(subject.task :bootstrap).to eq(
42
+ 'PowerShell -NoProfile -ExecutionPolicy Bypass -File "scripts/bootstrap.ps1"'
43
+ )
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ context 'not matching a task' do
50
+ it 'raises an error' do
51
+ # Use foo to ensure it doesn't match ps1 or hidden (. prefixed) files
52
+ expect(subject.task :foo).to be nil
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,91 @@
1
+ module Omnitest
2
+ class Psychic
3
+ module Factories
4
+ RSpec.describe BundlerTaskFactory do
5
+ let(:psychic) { Psychic.new(cwd: current_dir) }
6
+ let(:shell) { Omnitest::Shell.shell = double('shell') }
7
+ subject { described_class.new(psychic, cwd: current_dir) }
8
+
9
+ shared_context 'without bundler' do
10
+ before(:each) do
11
+ ENV['BUNDLE_GEMFILE'] = nil
12
+ end
13
+ end
14
+
15
+ shared_context 'with Gemfile' do
16
+ before(:each) do
17
+ ENV['BUNDLE_GEMFILE'] = nil
18
+ write_file 'Gemfile', ''
19
+ end
20
+ end
21
+
22
+ shared_context 'with .bundle/config' do
23
+ before(:each) do
24
+ ENV['BUNDLE_GEMFILE'] = nil
25
+ write_file '.bundle/config', <<-eos
26
+ ---
27
+ BUNDLE_GEMFILE: "../../Gemfile"
28
+ eos
29
+ end
30
+ end
31
+
32
+ shared_context 'with BUNDLE_GEMFILE environment variable' do
33
+ around(:each) do | example |
34
+ ENV['BUNDLE_GEMFILE'] = 'Gemfile'
35
+ example.run
36
+ ENV['BUNDLE_GEMFILE'] = nil
37
+ end
38
+ end
39
+
40
+ shared_examples 'does not use bundler' do
41
+ describe 'active?' do
42
+ it 'returns false' do
43
+ expect(subject.active?).to be false
44
+ end
45
+ end
46
+ end
47
+
48
+ shared_examples 'uses bundler' do
49
+ describe 'known_task?' do
50
+ it 'is true for bootstrap' do
51
+ expect(subject.known_task? :bootstrap).to be true
52
+ end
53
+ it 'is false for compile' do
54
+ expect(subject.known_task? :compile).to be false
55
+ end
56
+ end
57
+
58
+ describe '#bootstrap' do
59
+ it 'returns bundle install' do
60
+ expect(subject.task(:bootstrap)).to eq('bundle install')
61
+ end
62
+ end
63
+ end
64
+
65
+ context 'with Gemfile' do
66
+ include_context 'with Gemfile' do
67
+ include_examples 'uses bundler'
68
+ end
69
+ end
70
+
71
+ context 'with .bundle/config' do
72
+ include_context 'with .bundle/config' do
73
+ include_examples 'uses bundler'
74
+ end
75
+ end
76
+
77
+ context 'with BUNDLE_GEMFILE environment variable' do
78
+ include_context 'with BUNDLE_GEMFILE environment variable' do
79
+ include_examples 'uses bundler'
80
+ end
81
+ end
82
+
83
+ context 'without bundler' do
84
+ include_context 'without bundler' do
85
+ include_examples 'does not use bundler'
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,79 @@
1
+ module Omnitest
2
+ class Psychic
3
+ module Factories
4
+ RSpec.describe ShellTaskFactory do
5
+ let(:psychic) { Psychic.new(cwd: current_dir) }
6
+ let(:shell) { Omnitest::Shell.shell = double('shell') }
7
+ subject { described_class.new(psychic) }
8
+
9
+ shared_context 'with scripts/*.sh files' do
10
+ before(:each) do
11
+ write_file 'scripts/bootstrap.sh', ''
12
+ write_file 'scripts/compile.sh', ''
13
+ write_file 'scripts/foo.ps1', ''
14
+ end
15
+ end
16
+
17
+ shared_context 'with scripts/* (no extension) files' do
18
+ before(:each) do
19
+ write_file 'scripts/bootstrap', ''
20
+ write_file 'scripts/compile', ''
21
+ write_file 'scripts/.foo', ''
22
+ end
23
+ end
24
+
25
+ describe 'known_task?' do
26
+ shared_examples 'detects matching scripts' do
27
+ it 'returns true if a matching script exists' do
28
+ expect(subject.known_task? :bootstrap).to be true
29
+ expect(subject.known_task? :compile).to be true
30
+ end
31
+ it 'returns false if a matching script does not exists' do
32
+ expect(subject.known_task? :foo).to be false
33
+ expect(subject.known_task? :bar).to be false
34
+ end
35
+ end
36
+
37
+ context 'with scripts/*.sh files' do
38
+ include_context 'with scripts/*.sh files' do
39
+ include_examples 'detects matching scripts'
40
+ end
41
+ end
42
+
43
+ context 'with scripts/* (no extension) files' do
44
+ include_context 'with scripts/* (no extension) files' do
45
+ include_examples 'detects matching scripts'
46
+ end
47
+ end
48
+ end
49
+
50
+ describe '#task' do
51
+ context 'matching a task' do
52
+ context 'with scripts/*.sh files' do
53
+ include_context 'with scripts/*.sh files' do
54
+ it 'returns the script command' do
55
+ expect(subject.task :bootstrap).to eq('./scripts/bootstrap.sh')
56
+ end
57
+ end
58
+ end
59
+
60
+ context 'with scripts/* (no extension) files' do
61
+ include_context 'with scripts/* (no extension) files' do
62
+ it 'executes the script command' do
63
+ expect(subject.task :bootstrap).to eq('./scripts/bootstrap')
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ context 'not matching a task' do
70
+ it 'raises an error' do
71
+ # Use foo to ensure it doesn't match ps1 or hidden (. prefixed) files
72
+ expect(subject.task :foo).to be nil
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,78 @@
1
+ module Omnitest
2
+ class Psychic
3
+ module Factories
4
+ RSpec.describe TravisTaskFactory do
5
+ let(:psychic) { Psychic.new(cwd: current_dir) }
6
+ let(:shell) { double('shell') }
7
+ subject do
8
+ psychic.shell = shell
9
+ described_class.new(psychic, cwd: current_dir)
10
+ end
11
+
12
+ shared_context 'without .travis.yml' do
13
+ end
14
+
15
+ shared_context 'with .travis.yml' do
16
+ before(:each) do
17
+ ENV['BUNDLE_GEMFILE'] = nil
18
+ write_file '.travis.yml', ''
19
+ end
20
+ end
21
+
22
+ shared_examples 'does not use Travis' do
23
+ describe 'active?' do
24
+ it 'returns false' do
25
+ expect(subject.active?).to be false
26
+ end
27
+ end
28
+ end
29
+
30
+ shared_examples 'uses Travis' do
31
+ describe 'known_task?' do
32
+ it 'is true for bootstrap' do
33
+ expect(subject.known_task? :bootstrap).to be true
34
+ end
35
+ it 'is true for test' do
36
+ expect(subject.known_task? :unittest).to be true
37
+ end
38
+ it 'is false for lint' do
39
+ expect(subject.known_task? :lint).to be false
40
+ end
41
+ end
42
+
43
+ # TODO: Do we want to return the travis command itself, or the result of `travis run -p`?
44
+
45
+ describe '#bootstrap' do
46
+ it 'returns travis run install' do
47
+ expect(shell).to receive(:execute).with('travis run --print --skip-version-check install', cwd: current_dir).and_return(
48
+ Fabricate(:execution_result, stdout: 'script from travis')
49
+ )
50
+ expect(subject.task(:bootstrap)).to eq('script from travis')
51
+ end
52
+ end
53
+
54
+ describe '#unittest' do
55
+ it 'returns travis run script' do
56
+ expect(shell).to receive(:execute).with('travis run --print --skip-version-check script', cwd: current_dir).and_return(
57
+ Fabricate(:execution_result, stdout: 'script from travis')
58
+ )
59
+ expect(subject.task(:unittest)).to eq('script from travis')
60
+ end
61
+ end
62
+ end
63
+
64
+ context 'with .travis.yml' do
65
+ include_context 'with .travis.yml' do
66
+ include_examples 'uses Travis'
67
+ end
68
+ end
69
+
70
+ context 'without .travis.yml' do
71
+ include_context 'without .travis.yml' do
72
+ include_examples 'does not use Travis'
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,51 @@
1
+ module Omnitest
2
+ class Psychic
3
+ module Factories
4
+ RSpec.describe HotReadTaskFactory do
5
+ let(:task_map) do
6
+ {
7
+ 'bootstrap' => 'foo',
8
+ 'compile' => 'bar',
9
+ 'execute' => 'baz'
10
+ }
11
+ end
12
+ let(:hints) do
13
+ Hints.new(
14
+ 'tasks' => task_map
15
+ )
16
+ end
17
+ let(:psychic) { double('psychic', hints: hints) }
18
+ let(:shell) { Omnitest::Shell.shell = double('shell') }
19
+ subject { described_class.new(psychic, cwd: current_dir) }
20
+
21
+ describe 'known_task?' do
22
+ it 'returns true for task ids' do
23
+ task_map.each_key do |key|
24
+ expect(subject.known_task? key).to be true
25
+ end
26
+ end
27
+
28
+ it 'returns false for anything else' do
29
+ expect(subject.known_task? 'max').to be false
30
+ end
31
+ end
32
+
33
+ describe '#task' do
34
+ context 'matching a task' do
35
+ it 'builds the task command' do
36
+ expect(subject.task(:bootstrap)).to eq('foo')
37
+ end
38
+
39
+ pending 'test priority when multiple factories implement run_script'
40
+ end
41
+
42
+ context 'not matching a task' do
43
+ it 'raises an error' do
44
+ expect { subject.spin_around }.to raise_error(NoMethodError)
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ module Omnitest
4
+ class Psychic
5
+ class FakeRubyFactory < ScriptFactory
6
+ runs '**.fake_rb'
7
+ runs '**.fake_erb'
8
+ end
9
+
10
+ class FakeJavaScriptFactory < ScriptFactory
11
+ runs '**.fake_js'
12
+ end
13
+
14
+ RSpec.describe ScriptFactoryManager do
15
+ let(:psychic) { Psychic.new(cwd: current_dir) }
16
+ let(:opts) do
17
+ {}
18
+ end
19
+ let(:subject) do
20
+ described_class.new psychic, opts
21
+ end
22
+
23
+ before(:each) do
24
+ described_class.register_factory(FakeRubyFactory)
25
+ described_class.register_factory(FakeJavaScriptFactory)
26
+ end
27
+
28
+ def fake_script(name)
29
+ write_file name, ''
30
+ psychic.script name
31
+ end
32
+
33
+ describe '#initialize' do
34
+ it 'creates instances of registered factories' do
35
+ expect(subject.factories).to include(
36
+ an_instance_of FakeRubyFactory
37
+ )
38
+ end
39
+ end
40
+
41
+ describe '#factories_for' do
42
+ it 'returns nil if no factories can run the script' do
43
+ expect(subject.factories_for fake_script('foo.asdf')).to be_empty
44
+ end
45
+
46
+ it 'returns the factories that can run the script' do
47
+ expect(subject.factories_for fake_script('foo.fake_rb')).to include(
48
+ an_instance_of FakeRubyFactory
49
+ )
50
+ expect(subject.factories_for fake_script('foo.fake_js')).to include(
51
+ an_instance_of FakeJavaScriptFactory
52
+ )
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ module Omnitest
4
+ class Psychic
5
+ RSpec.describe Script do
6
+ before(:each) do
7
+ write_file 'sample.rb', <<-eos
8
+ puts 'hello'
9
+ puts '{basic_token}'
10
+ puts '{{mustache_token}}'
11
+ puts ENV['ENV_VAR']
12
+ # --flag
13
+ opts = {}
14
+ # Pretend we parsed opts
15
+ puts opts[:flag]
16
+ eos
17
+ end
18
+
19
+ subject { Psychic.new(cwd: current_dir).script('sample') }
20
+
21
+ describe '#tokens' do
22
+ it 'is empty if the there is no detection_strategy' do
23
+ subject.opts[:detection_strategy] = nil
24
+ expect(subject.tokens).to be_empty
25
+ end
26
+
27
+ it 'finds the basic_token if the execution strategy is tokens' do
28
+ subject.opts[:detection_strategy] = 'tokens'
29
+ expect(subject.tokens).to eq(['basic_token'])
30
+ end
31
+ end
32
+
33
+ describe '#execution_strategy' do
34
+ it 'is a DefaultStrategy if the mode is not set' do
35
+ expect(subject.execution_strategy).to be_an_instance_of Execution::DefaultStrategy
36
+ end
37
+
38
+ it 'is an EnvStrategy if the mode is env' do
39
+ subject.opts[:execution_strategy] = 'environment_variables'
40
+ expect(subject.execution_strategy).to be_an_instance_of Execution::EnvStrategy
41
+ end
42
+
43
+ it 'is a FlagStrategy if the mode is flags' do
44
+ subject.opts[:execution_strategy] = 'flags'
45
+ expect(subject.execution_strategy).to be_an_instance_of Execution::FlagStrategy
46
+ end
47
+
48
+ it 'is a TokenStrategy if the mode is tokens' do
49
+ subject.opts[:execution_strategy] = 'tokens'
50
+ expect(subject.execution_strategy).to be_an_instance_of Execution::TokenStrategy
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end