delta_test 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +15 -0
  3. data/.rspec +4 -0
  4. data/.ruby-version +1 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +165 -0
  8. data/Rakefile +17 -0
  9. data/bin/delta_test +12 -0
  10. data/circle.yml +12 -0
  11. data/delta_test.gemspec +30 -0
  12. data/lib/delta_test/analyzer.rb +47 -0
  13. data/lib/delta_test/cli.rb +224 -0
  14. data/lib/delta_test/configuration.rb +173 -0
  15. data/lib/delta_test/dependencies_table.rb +83 -0
  16. data/lib/delta_test/errors.rb +55 -0
  17. data/lib/delta_test/generator.rb +101 -0
  18. data/lib/delta_test/git.rb +88 -0
  19. data/lib/delta_test/related_spec_list.rb +64 -0
  20. data/lib/delta_test/spec_helpers.rb +42 -0
  21. data/lib/delta_test/utils.rb +93 -0
  22. data/lib/delta_test/version.rb +9 -0
  23. data/lib/delta_test.rb +47 -0
  24. data/spec/fixtures/sample/alpha.rb +19 -0
  25. data/spec/fixtures/sample/beta.rb +15 -0
  26. data/spec/fixtures/sample/gamma.rb +9 -0
  27. data/spec/lib/delta_test/analyzer_spec.rb +126 -0
  28. data/spec/lib/delta_test/cli_spec.rb +422 -0
  29. data/spec/lib/delta_test/configuration_spec.rb +353 -0
  30. data/spec/lib/delta_test/dependencies_table_spec.rb +129 -0
  31. data/spec/lib/delta_test/generator_spec.rb +201 -0
  32. data/spec/lib/delta_test/git_spec.rb +178 -0
  33. data/spec/lib/delta_test/related_spec_list_spec.rb +182 -0
  34. data/spec/lib/delta_test/spec_helpers_spec.rb +72 -0
  35. data/spec/lib/delta_test/utils_spec.rb +244 -0
  36. data/spec/lib/delta_test_spec.rb +119 -0
  37. data/spec/rails/.gitignore +19 -0
  38. data/spec/rails/.rspec +3 -0
  39. data/spec/rails/Gemfile +15 -0
  40. data/spec/rails/Gemfile.lock +163 -0
  41. data/spec/rails/README.rdoc +28 -0
  42. data/spec/rails/Rakefile +6 -0
  43. data/spec/rails/app/controllers/application_controller.rb +5 -0
  44. data/spec/rails/app/controllers/concerns/.keep +0 -0
  45. data/spec/rails/app/helpers/application_helper.rb +2 -0
  46. data/spec/rails/app/mailers/.keep +0 -0
  47. data/spec/rails/app/models/.keep +0 -0
  48. data/spec/rails/app/models/concerns/.keep +0 -0
  49. data/spec/rails/app/views/layouts/application.html.haml +7 -0
  50. data/spec/rails/bin/bundle +3 -0
  51. data/spec/rails/bin/rails +4 -0
  52. data/spec/rails/bin/rake +4 -0
  53. data/spec/rails/bin/setup +29 -0
  54. data/spec/rails/config/application.rb +35 -0
  55. data/spec/rails/config/boot.rb +3 -0
  56. data/spec/rails/config/database.yml +25 -0
  57. data/spec/rails/config/environment.rb +5 -0
  58. data/spec/rails/config/environments/development.rb +41 -0
  59. data/spec/rails/config/environments/production.rb +79 -0
  60. data/spec/rails/config/environments/test.rb +42 -0
  61. data/spec/rails/config/initializers/assets.rb +11 -0
  62. data/spec/rails/config/initializers/backtrace_silencers.rb +7 -0
  63. data/spec/rails/config/initializers/cookies_serializer.rb +3 -0
  64. data/spec/rails/config/initializers/filter_parameter_logging.rb +4 -0
  65. data/spec/rails/config/initializers/inflections.rb +16 -0
  66. data/spec/rails/config/initializers/mime_types.rb +4 -0
  67. data/spec/rails/config/initializers/session_store.rb +3 -0
  68. data/spec/rails/config/initializers/wrap_parameters.rb +14 -0
  69. data/spec/rails/config/locales/en.yml +23 -0
  70. data/spec/rails/config/routes.rb +56 -0
  71. data/spec/rails/config/secrets.yml +22 -0
  72. data/spec/rails/config.ru +4 -0
  73. data/spec/rails/db/seeds.rb +7 -0
  74. data/spec/rails/delta_test.yml +5 -0
  75. data/spec/rails/lib/assets/.keep +0 -0
  76. data/spec/rails/lib/tasks/.keep +0 -0
  77. data/spec/rails/log/.keep +0 -0
  78. data/spec/rails/public/404.html +67 -0
  79. data/spec/rails/public/422.html +67 -0
  80. data/spec/rails/public/500.html +66 -0
  81. data/spec/rails/public/favicon.ico +0 -0
  82. data/spec/rails/public/robots.txt +5 -0
  83. data/spec/rails/spec/features/sample_spec.rb +7 -0
  84. data/spec/rails/spec/spec_helper.rb +16 -0
  85. data/spec/rails/vendor/assets/javascripts/.keep +0 -0
  86. data/spec/rails/vendor/assets/stylesheets/.keep +0 -0
  87. data/spec/spec_helper.rb +39 -0
  88. data/spec/supports/create_table_file.rb +21 -0
  89. metadata +283 -0
@@ -0,0 +1,178 @@
1
+ require 'delta_test/git'
2
+
3
+ describe DeltaTest::Git do
4
+
5
+ let(:out) { '' }
6
+ let(:success_status) { [out, '', double(success?: true)] }
7
+ let(:error_status) { ['', '', double(success?: false)] }
8
+
9
+ describe '::git_repo?' do
10
+
11
+ let(:command) { %q{git rev-parse --is-inside-work-tree} }
12
+
13
+ it 'should return false if `git` command is not exist' do
14
+ allow(Open3).to receive(:capture3).with(command, any_args).and_raise
15
+
16
+ result = nil
17
+
18
+ expect {
19
+ result = DeltaTest::Git.git_repo?
20
+ }.not_to raise_error
21
+
22
+ expect(result).to be(false)
23
+ end
24
+
25
+ it 'should return true exit code is 0' do
26
+ allow(Open3).to receive(:capture3).with(command, any_args).and_return(success_status)
27
+
28
+ expect(DeltaTest::Git.git_repo?).to be(true)
29
+ end
30
+
31
+ it 'should return false exit code not is 0' do
32
+ allow(Open3).to receive(:capture3).with(command, any_args).and_return(error_status)
33
+
34
+ expect(DeltaTest::Git.git_repo?).to be(false)
35
+ end
36
+
37
+ end
38
+
39
+ describe '::root_dir' do
40
+
41
+ let(:command) { %q{git rev-parse --show-toplevel} }
42
+ let(:out) { '/root/dir' }
43
+
44
+ it 'should raise an error if the command is not exist' do
45
+ allow(Open3).to receive(:capture3).with(command, any_args).and_raise
46
+
47
+ expect {
48
+ DeltaTest::Git.root_dir
49
+ }.to raise_error
50
+ end
51
+
52
+ it 'should return a root directory path if success' do
53
+ allow(Open3).to receive(:capture3).with(command, any_args).and_return(success_status)
54
+
55
+ expect(DeltaTest::Git.root_dir).to eq(out)
56
+ end
57
+
58
+ it 'should return nil if error' do
59
+ allow(Open3).to receive(:capture3).with(command, any_args).and_return(error_status)
60
+
61
+ expect(DeltaTest::Git.root_dir).to be_nil
62
+ end
63
+
64
+ end
65
+
66
+ describe '::rev_parse' do
67
+
68
+ let(:rev) { 'HEAD' }
69
+ let(:command) { %Q{git rev-parse #{rev}} }
70
+ let(:out) { '818b60efa12b4bd99815e9b550185d1fb6244663' }
71
+
72
+ it 'should raise an error if the command is not exist' do
73
+ allow(Open3).to receive(:capture3).with(command, any_args).and_raise
74
+
75
+ expect {
76
+ DeltaTest::Git.rev_parse(rev)
77
+ }.to raise_error
78
+ end
79
+
80
+ it 'should return a commit id if success' do
81
+ allow(Open3).to receive(:capture3).with(command, any_args).and_return(success_status)
82
+
83
+ expect(DeltaTest::Git.rev_parse(rev)).to eq(out)
84
+ end
85
+
86
+ it 'should return nil if error' do
87
+ allow(Open3).to receive(:capture3).with(command, any_args).and_return(error_status)
88
+
89
+ expect(DeltaTest::Git.rev_parse(rev)).to be_nil
90
+ end
91
+
92
+ end
93
+
94
+ describe '::same_commit?' do
95
+
96
+ let(:map) do
97
+ {
98
+ 'master' => '0000000000000000000000000000000000000000',
99
+ 'HEAD' => '1111111111111111111111111111111111111111',
100
+ 'feature/foo' => '1111111111111111111111111111111111111111',
101
+ }
102
+ end
103
+
104
+ before do
105
+ map.each do |name, commit_id|
106
+ allow(DeltaTest::Git).to receive(:rev_parse).with(name).and_return(commit_id)
107
+ end
108
+ end
109
+
110
+ it 'should compare two names by thier commit ids' do
111
+ names = map.values
112
+ names.product(names).each do |r1, r2|
113
+ expect(DeltaTest::Git).to receive(:rev_parse).with(r1).ordered
114
+ expect(DeltaTest::Git).to receive(:rev_parse).with(r2).ordered
115
+
116
+ is_same = (map[r1] == map[r2])
117
+
118
+ expect(DeltaTest::Git.same_commit?(r1, r2)).to be(is_same)
119
+ end
120
+ end
121
+
122
+ end
123
+
124
+ describe '::ls_files' do
125
+
126
+ let(:command) { %q{git ls-files -z} }
127
+ let(:out) { "/a/file/1\x0/a/file/2\x0/a/file/3" }
128
+
129
+ it 'should raise an error if the command is not exist' do
130
+ allow(Open3).to receive(:capture3).with(command, any_args).and_raise
131
+
132
+ expect {
133
+ DeltaTest::Git.ls_files
134
+ }.to raise_error
135
+ end
136
+
137
+ it 'should return an array if success' do
138
+ allow(Open3).to receive(:capture3).with(command, any_args).and_return(success_status)
139
+
140
+ expect(DeltaTest::Git.ls_files).to eq(out.split("\x0"))
141
+ end
142
+
143
+ it 'should return an empty array if error' do
144
+ allow(Open3).to receive(:capture3).with(command, any_args).and_return(error_status)
145
+
146
+ expect(DeltaTest::Git.ls_files).to eq([])
147
+ end
148
+
149
+ end
150
+
151
+ describe '::changed_files' do
152
+
153
+ let(:command) { %q{git --no-pager diff --name-only -z master HEAD} }
154
+ let(:out) { "/a/file/1\x0/a/file/2\x0/a/file/3" }
155
+
156
+ it 'should raise an error if the command is not exist' do
157
+ allow(Open3).to receive(:capture3).with(command, any_args).and_raise
158
+
159
+ expect {
160
+ DeltaTest::Git.changed_files
161
+ }.to raise_error
162
+ end
163
+
164
+ it 'should return an array if success' do
165
+ allow(Open3).to receive(:capture3).with(command, any_args).and_return(success_status)
166
+
167
+ expect(DeltaTest::Git.changed_files).to eq(out.split("\x0"))
168
+ end
169
+
170
+ it 'should return an empty array if error' do
171
+ allow(Open3).to receive(:capture3).with(command, any_args).and_return(error_status)
172
+
173
+ expect(DeltaTest::Git.changed_files).to eq([])
174
+ end
175
+
176
+ end
177
+
178
+ end
@@ -0,0 +1,182 @@
1
+ require 'delta_test/related_spec_list'
2
+
3
+ describe DeltaTest::RelatedSpecList do
4
+
5
+ include_examples :defer_create_table_file
6
+
7
+ let(:base) { 'master' }
8
+ let(:head) { 'feature/foo' }
9
+ let(:list) { DeltaTest::RelatedSpecList.new }
10
+
11
+ let(:base_path) { '/base_path' }
12
+
13
+ before do
14
+ DeltaTest.configure do |config|
15
+ config.base_path = base_path
16
+ config.table_file = table_file_path
17
+ end
18
+ end
19
+
20
+ shared_examples :_mock_table_and_changed_files do
21
+
22
+ let(:table) do
23
+ table = DeltaTest::DependenciesTable.new
24
+
25
+ table['spec/foo_spec.rb'] << 'lib/foo.rb'
26
+ table['spec/bar_spec.rb'] << 'lib/bar.rb'
27
+ table['spec/baz_spec.rb'] << 'lib/baz.rb'
28
+ table['spec/other_spec.rb']
29
+ table['spec/mixed_spec.rb'] << 'lib/foo.rb'
30
+ table['spec/mixed_spec.rb'] << 'lib/bar.rb'
31
+
32
+ table
33
+ end
34
+
35
+ let(:changed_files) do
36
+ [
37
+ 'lib/foo.rb',
38
+ ]
39
+ end
40
+
41
+ before do
42
+ allow(DeltaTest::DependenciesTable).to receive(:load).with(Pathname.new(table_file_path)).and_return(table)
43
+
44
+ allow(DeltaTest::Git).to receive(:git_repo?).and_return(true)
45
+ allow(DeltaTest::Git).to receive(:changed_files).with(base, head).and_return(changed_files)
46
+ end
47
+
48
+ end
49
+
50
+ describe '#load_table!' do
51
+
52
+ it 'should raise an error if a table file is not exist' do
53
+ expect {
54
+ list.load_table!
55
+ }.to raise_error(DeltaTest::TableNotFoundError)
56
+ end
57
+
58
+ it 'should load the table if exist' do
59
+ table_file
60
+
61
+ expect(list.table).to be_nil
62
+
63
+ expect {
64
+ list.load_table!
65
+ }.not_to raise_error
66
+
67
+ expect(list.table).to be_a(DeltaTest::DependenciesTable)
68
+ end
69
+
70
+ end
71
+
72
+ describe '#retrive_changed_files!' do
73
+
74
+ include_examples :_mock_table_and_changed_files
75
+
76
+ it 'shoud raise an error if the directory is not managed by git' do
77
+ allow(DeltaTest::Git).to receive(:git_repo?).and_return(false)
78
+
79
+ expect {
80
+ list.retrive_changed_files!(base, head)
81
+ }.to raise_error(DeltaTest::NotInGitRepositoryError)
82
+ end
83
+
84
+ it 'shoud retrive a list of changed files' do
85
+ expect(list.changed_files).to be_nil
86
+
87
+ list.retrive_changed_files!(base, head)
88
+
89
+ expect(list.changed_files).to be_a(Array)
90
+ expect(list.changed_files).not_to be_empty
91
+ end
92
+
93
+ end
94
+
95
+ describe '#related_spec_files' do
96
+
97
+ include_examples :_mock_table_and_changed_files
98
+
99
+ before do
100
+ table_file
101
+ list.load_table!
102
+ list.retrive_changed_files!(base, head)
103
+ end
104
+
105
+ describe 'Dependents' do
106
+
107
+ let(:related_spec_files) do
108
+ Set[
109
+ 'spec/foo_spec.rb',
110
+ 'spec/mixed_spec.rb',
111
+ ]
112
+ end
113
+
114
+ it 'should be included' do
115
+ expect(list.related_spec_files).to eq(related_spec_files)
116
+ end
117
+
118
+ end
119
+
120
+ describe 'Modified spec files' do
121
+
122
+ let(:changed_files) do
123
+ [
124
+ 'lib/foo.rb',
125
+ 'spec/baz_spec.rb',
126
+ ]
127
+ end
128
+
129
+ let(:related_spec_files) do
130
+ Set[
131
+ 'spec/foo_spec.rb',
132
+ 'spec/mixed_spec.rb',
133
+ 'spec/baz_spec.rb',
134
+ ]
135
+ end
136
+
137
+ it 'should be included' do
138
+ expect(list.related_spec_files).to eq(related_spec_files)
139
+ end
140
+
141
+ end
142
+
143
+ describe 'Custom dependents' do
144
+
145
+ let(:custom_mappings) do
146
+ {
147
+ 'spec/other_spec.rb' => [
148
+ 'config/locales/**/*.yml',
149
+ ]
150
+ }
151
+ end
152
+
153
+ let(:changed_files) do
154
+ [
155
+ 'lib/foo.rb',
156
+ 'config/locales/something/en.yml',
157
+ ]
158
+ end
159
+
160
+ let(:related_spec_files) do
161
+ Set[
162
+ 'spec/foo_spec.rb',
163
+ 'spec/mixed_spec.rb',
164
+ 'spec/other_spec.rb',
165
+ ]
166
+ end
167
+
168
+ before do
169
+ DeltaTest.configure do |config|
170
+ config.custom_mappings = custom_mappings
171
+ end
172
+ end
173
+
174
+ it 'should be included' do
175
+ expect(list.related_spec_files).to eq(related_spec_files)
176
+ end
177
+
178
+ end
179
+
180
+ end
181
+
182
+ end
@@ -0,0 +1,72 @@
1
+ require 'delta_test/spec_helpers'
2
+
3
+ describe DeltaTest::SpecHelpers do
4
+
5
+ before do
6
+ @rspec_example_group = Class.new do
7
+ class << self
8
+ def before(_)
9
+ yield if block_given?
10
+ end
11
+
12
+ def after(_)
13
+ yield if block_given?
14
+ end
15
+
16
+ def metadata
17
+ { file_path: 'spec/foo/bar.rb' }
18
+ end
19
+ end
20
+ end
21
+
22
+ allow(DeltaTest).to receive(:active?).and_return(false)
23
+ end
24
+
25
+ it 'should define a global generator' do
26
+ expect(defined?($delta_test_generator)).not_to be(false)
27
+ end
28
+
29
+ describe 'when extending' do
30
+
31
+ context 'before :all' do
32
+
33
+ it 'should call it' do
34
+ expect(@rspec_example_group).to receive(:before).with(:context)
35
+ @rspec_example_group.extend DeltaTest::SpecHelpers
36
+ end
37
+
38
+ it 'should start the generator' do
39
+ expect($delta_test_generator).to receive(:start!).with("spec/foo/bar.rb")
40
+ @rspec_example_group.extend DeltaTest::SpecHelpers
41
+ end
42
+
43
+ end
44
+
45
+ context 'after :all' do
46
+
47
+ it 'should call it' do
48
+ expect(@rspec_example_group).to receive(:after).with(:context)
49
+ @rspec_example_group.extend DeltaTest::SpecHelpers
50
+ end
51
+
52
+ it 'should stop the generator' do
53
+ expect($delta_test_generator).to receive(:stop!)
54
+ @rspec_example_group.extend DeltaTest::SpecHelpers
55
+ end
56
+
57
+ end
58
+
59
+ end
60
+
61
+ describe 'when including' do
62
+
63
+ it 'should call it' do
64
+ expect(@rspec_example_group).to receive(:before)
65
+ @rspec_example_group.class_eval do
66
+ include DeltaTest::SpecHelpers
67
+ end
68
+ end
69
+
70
+ end
71
+
72
+ end
@@ -0,0 +1,244 @@
1
+ require 'delta_test/utils'
2
+
3
+ describe DeltaTest::Utils do
4
+
5
+ describe '::regulate_filepath' do
6
+
7
+ let(:base_path) { Pathname.new('/base_path') }
8
+
9
+ it 'shoud return a relative path from `base_path`' do
10
+ absolute_path = Pathname.new('/base_path/foo/file_1.txt')
11
+ relative_path = Pathname.new('foo/file_1.txt')
12
+
13
+ expect(DeltaTest::Utils.regulate_filepath(absolute_path, base_path)).to eq(relative_path)
14
+ end
15
+
16
+ it 'shoud return a clean path' do
17
+ absolute_path = Pathname.new('./foo/file_1.txt')
18
+ relative_path = Pathname.new('foo/file_1.txt')
19
+
20
+ expect(DeltaTest::Utils.regulate_filepath(absolute_path, base_path)).to eq(relative_path)
21
+ end
22
+
23
+ it 'shoud not raise an error and return the path when a path is not started with `base_path`' do
24
+ path = Pathname.new('other/foo/file_1.txt')
25
+
26
+ expect(DeltaTest::Utils.regulate_filepath(path, base_path)).to eq(path)
27
+ end
28
+
29
+ end
30
+
31
+ describe '::find_file_upward' do
32
+
33
+ let(:file) { FakeFS::FakeFile.new }
34
+ let(:file_name) { 'file' }
35
+
36
+ it 'should return a file path if a file is exist in the current directory' do
37
+ pwd = '/a/b/c/d'
38
+ file_path = "/a/b/c/d/#{file_name}"
39
+
40
+ FakeFS::FileSystem.add(pwd)
41
+ FakeFS::FileSystem.add(file_path, file)
42
+
43
+ Dir.chdir(pwd) do
44
+ expect(DeltaTest::Utils.find_file_upward(file_name)).to eq(file_path)
45
+ end
46
+ end
47
+
48
+ it 'should return a file path if a file is exist at the parent directory' do
49
+ pwd = '/a/b/c/d'
50
+ file_path = "/a/b/c/#{file_name}"
51
+
52
+ FakeFS::FileSystem.add(pwd)
53
+ FakeFS::FileSystem.add(file_path, file)
54
+
55
+ Dir.chdir(pwd) do
56
+ expect(DeltaTest::Utils.find_file_upward(file_name)).to eq(file_path)
57
+ end
58
+ end
59
+
60
+ it 'should return a file path if a file is exist at somewhere of parent directories' do
61
+ pwd = '/a/b/c/d'
62
+ file_path = "/a/#{file_name}"
63
+
64
+ FakeFS::FileSystem.add(pwd)
65
+ FakeFS::FileSystem.add(file_path, file)
66
+
67
+ Dir.chdir(pwd) do
68
+ expect(DeltaTest::Utils.find_file_upward(file_name)).to eq(file_path)
69
+ end
70
+ end
71
+
72
+ it 'should return nil if a file is not exist in any parent directories' do
73
+ pwd = '/a/b/c/d'
74
+ file_path = "/abc/#{file_name}"
75
+
76
+ FakeFS::FileSystem.add(pwd)
77
+ FakeFS::FileSystem.add(file_path, file)
78
+
79
+ Dir.chdir(pwd) do
80
+ expect(DeltaTest::Utils.find_file_upward(file_name)).to be_nil
81
+ end
82
+ end
83
+
84
+ context 'Multiple file names' do
85
+
86
+ let(:file_2) { FakeFS::FakeFile.new }
87
+ let(:file_name_2) { 'file' }
88
+
89
+ it 'should return a file path if one of files is exist at somewhere of parent directories' do
90
+ pwd = '/a/b/c/d'
91
+ file_path = "/a/#{file_name}"
92
+
93
+ FakeFS::FileSystem.add(pwd)
94
+ FakeFS::FileSystem.add(file_path, file)
95
+
96
+ Dir.chdir(pwd) do
97
+ expect(DeltaTest::Utils.find_file_upward(file_name, file_name_2)).to eq(file_path)
98
+ end
99
+ end
100
+
101
+ it 'should return the first match if one of files is exist at somewhere of parent directories' do
102
+ pwd = '/a/b/c/d'
103
+ file_path = "/a/#{file_name}"
104
+ file_path_2 = "/a/#{file_name_2}"
105
+
106
+ FakeFS::FileSystem.add(pwd)
107
+ FakeFS::FileSystem.add(file_path, file)
108
+ FakeFS::FileSystem.add(file_path_2, file)
109
+
110
+ Dir.chdir(pwd) do
111
+ expect(DeltaTest::Utils.find_file_upward(file_name, file_name_2)).to eq(file_path)
112
+ expect(DeltaTest::Utils.find_file_upward(file_name_2, file_name)).to eq(file_path_2)
113
+ end
114
+ end
115
+
116
+ it 'should return nil if non of files is not exist in any parent directories' do
117
+ pwd = '/a/b/c/d'
118
+ file_path = "/abc/#{file_name}"
119
+ file_path_2 = "/cba/#{file_name_2}"
120
+
121
+ FakeFS::FileSystem.add(pwd)
122
+ FakeFS::FileSystem.add(file_path, file)
123
+ FakeFS::FileSystem.add(file_path_2, file)
124
+
125
+ Dir.chdir(pwd) do
126
+ expect(DeltaTest::Utils.find_file_upward(file_name, file_name_2)).to be_nil
127
+ end
128
+ end
129
+
130
+ end
131
+
132
+ end
133
+
134
+ describe '::grep_pattern_to_regexp' do
135
+
136
+ # private method
137
+ let(:grep_pattern_to_regexp) { DeltaTest::Utils.method(:grep_pattern_to_regexp) }
138
+
139
+ it 'should return a Regexp' do
140
+ expect(grep_pattern_to_regexp.call('')).to be_a(Regexp)
141
+ end
142
+
143
+ it 'should wrap a pattern with ^ and $' do
144
+ regexp = grep_pattern_to_regexp.call('aaa')
145
+
146
+ expect(regexp).to be_a(Regexp)
147
+ expect(regexp.source).to eq('^aaa$')
148
+ end
149
+
150
+ it 'should escape any special characters in a pattern' do
151
+ regexp = grep_pattern_to_regexp.call('\?{}.')
152
+
153
+ expect(regexp).to be_a(Regexp)
154
+ expect(regexp.source).to eq('^\\\\\?\{\}\.$')
155
+ end
156
+
157
+ it 'should replace ** with super-directory wildcards' do
158
+ regexp = grep_pattern_to_regexp.call('a/**')
159
+
160
+ expect(regexp).to be_a(Regexp)
161
+ expect(regexp.source).to eq('^a/.*$')
162
+ end
163
+
164
+ it 'should care about trailling slash when **/' do
165
+ regexp = grep_pattern_to_regexp.call('a/**/path')
166
+
167
+ expect(regexp).to be_a(Regexp)
168
+ expect(regexp.source).to eq('^a/.*/?path$')
169
+ end
170
+
171
+ it 'should replace * with file/directory name wildcards' do
172
+ regexp = grep_pattern_to_regexp.call('a/*_file')
173
+
174
+ expect(regexp).to be_a(Regexp)
175
+ expect(regexp.source).to eq('^a/[^/]*_file$')
176
+ end
177
+
178
+ end
179
+
180
+ describe '::files_grep' do
181
+
182
+ let(:files) do
183
+ [
184
+ '/0',
185
+ '/a/b/c',
186
+ '/a/b/c/d',
187
+ '/a/b/c/d/e',
188
+ '/x/y/z',
189
+ ]
190
+ end
191
+
192
+ let(:patterns) do
193
+ [
194
+ '/a/**/*',
195
+ ]
196
+ end
197
+
198
+ let(:exclude_patterns) do
199
+ [
200
+ '/a/**/e',
201
+ '/x/y/*',
202
+ ]
203
+ end
204
+
205
+ it 'should return the whole files when the patterns is an empty array' do
206
+ expect(DeltaTest::Utils.files_grep(files, [])).to eq(files)
207
+ end
208
+
209
+ it 'should return the whole files when the exclude patterns is an empty array' do
210
+ expect(DeltaTest::Utils.files_grep(files, [], [])).to eq(files)
211
+ end
212
+
213
+ it 'should return files only matched with the patterns' do
214
+ matched_files = [
215
+ '/a/b/c',
216
+ '/a/b/c/d',
217
+ '/a/b/c/d/e',
218
+ ]
219
+
220
+ expect(DeltaTest::Utils.files_grep(files, patterns)).to eq(matched_files)
221
+ end
222
+
223
+ it 'should return files not matched with the exclude patterns' do
224
+ matched_files = [
225
+ '/0',
226
+ '/a/b/c',
227
+ '/a/b/c/d',
228
+ ]
229
+
230
+ expect(DeltaTest::Utils.files_grep(files, [], exclude_patterns)).to eq(matched_files)
231
+ end
232
+
233
+ it 'should return files not matched with an union of patterns' do
234
+ matched_files = [
235
+ '/a/b/c',
236
+ '/a/b/c/d',
237
+ ]
238
+
239
+ expect(DeltaTest::Utils.files_grep(files, patterns, exclude_patterns)).to eq(matched_files)
240
+ end
241
+
242
+ end
243
+
244
+ end