Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
09ad296e
Commit
09ad296e
authored
May 28, 2020
by
Albert Salim
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add test file finder for ruby files
parent
119a1900
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
152 additions
and
0 deletions
+152
-0
lib/quality/test_file_finder.rb
lib/quality/test_file_finder.rb
+62
-0
spec/lib/quality/test_file_finder_spec.rb
spec/lib/quality/test_file_finder_spec.rb
+90
-0
No files found.
lib/quality/test_file_finder.rb
0 → 100644
View file @
09ad296e
# frozen_string_literal: true
require
'ostruct'
module
Quality
class
TestFileFinder
RUBY_EXTENSION
=
'.rb'
EE_PREFIX
=
'ee/'
def
initialize
(
file
,
foss_test_only:
false
)
@file
=
file
@foss_test_only
=
foss_test_only
end
def
test_files
contexts
=
[
ee_context
,
foss_context
]
contexts
.
flat_map
do
|
context
|
match_test_files_for
(
context
)
end
end
private
attr_reader
:file
,
:foss_test_only
def
ee_context
OpenStruct
.
new
.
tap
do
|
ee
|
ee
.
app
=
%r{^
#{
EE_PREFIX
}
app/(.+)
\.
rb$}
unless
foss_test_only
ee
.
lib
=
%r{^
#{
EE_PREFIX
}
lib/(.+)
\.
rb$}
unless
foss_test_only
ee
.
spec_dir
=
"
#{
EE_PREFIX
}
spec"
unless
foss_test_only
ee
.
ee_modules
=
%r{^
#{
EE_PREFIX
}
(.*
\/
)ee/(.+)
\.
rb$}
ee
.
foss_spec_dir
=
'spec'
end
end
def
foss_context
OpenStruct
.
new
.
tap
do
|
foss
|
foss
.
app
=
%r{^app/(.+)
\.
rb$}
foss
.
lib
=
%r{^lib/(.+)
\.
rb$}
foss
.
spec_dir
=
'spec'
end
end
def
match_test_files_for
(
context
)
test_files
=
[]
if
(
match
=
context
.
app
&
.
match
(
file
))
test_files
<<
"
#{
context
.
spec_dir
}
/
#{
match
[
1
]
}
_spec.rb"
end
if
(
match
=
context
.
lib
&
.
match
(
file
))
test_files
<<
"
#{
context
.
spec_dir
}
/lib/
#{
match
[
1
]
}
_spec.rb"
end
if
(
match
=
context
.
ee_modules
&
.
match
(
file
))
test_files
<<
"
#{
context
.
foss_spec_dir
}
/
#{
match
[
1
]
}#{
match
[
2
]
}
_spec.rb"
end
test_files
end
end
end
spec/lib/quality/test_file_finder_spec.rb
0 → 100644
View file @
09ad296e
# frozen_string_literal: true
require
'fast_spec_helper'
describe
Quality
::
TestFileFinder
do
let
(
:file
)
{
'app/finders/admin/projects_finder.rb'
}
let
(
:test_files
)
{
[
'spec/finders/admin/projects_finder_spec.rb'
]
}
subject
{
Quality
::
TestFileFinder
.
new
(
file
)
}
shared_examples
'finding matching test files'
do
it
'returns matching test files'
do
expect
(
subject
.
test_files
).
to
match_array
(
test_files
)
end
end
shared_examples
'not finding a matching test file'
do
it
'returns empty array'
do
expect
(
subject
.
test_files
).
to
be_empty
end
end
describe
'#test_files'
do
it_behaves_like
'finding matching test files'
context
'when given non .rb files'
do
let
(
:file
)
{
'app/assets/images/emoji.png'
}
it_behaves_like
'not finding a matching test file'
end
context
'when given file in app/'
do
let
(
:file
)
{
'app/finders/admin/projects_finder.rb'
}
let
(
:test_files
)
{
[
'spec/finders/admin/projects_finder_spec.rb'
]
}
it_behaves_like
'finding matching test files'
end
context
'when given file in lib/'
do
let
(
:file
)
{
'lib/banzai/color_parser.rb'
}
let
(
:test_files
)
{
[
'spec/lib/banzai/color_parser_spec.rb'
]
}
it_behaves_like
'finding matching test files'
end
context
'when given a test file'
do
let
(
:file
)
{
'spec/lib/banzai/color_parser_spec.rb'
}
it_behaves_like
'not finding a matching test file'
end
context
'when given an ee app file'
do
let
(
:file
)
{
'ee/app/models/analytics/cycle_analytics/group_level.rb'
}
let
(
:test_files
)
{
[
'ee/spec/models/analytics/cycle_analytics/group_level_spec.rb'
]
}
it_behaves_like
'finding matching test files'
end
context
'when given an ee module file'
do
let
(
:file
)
{
'ee/app/models/ee/user.rb'
}
let
(
:test_files
)
{
[
'spec/app/models/user_spec.rb'
,
'ee/spec/models/ee/user_spec.rb'
]
}
it_behaves_like
'finding matching test files'
end
context
'when given an ee lib file'
do
let
(
:file
)
{
'ee/lib/flipper_session.rb'
}
let
(
:test_files
)
{
[
'ee/spec/lib/flipper_session_spec.rb'
]
}
it_behaves_like
'finding matching test files'
end
context
'when given an ee test file'
do
let
(
:file
)
{
'ee/spec/lib/banzai/color_parser_spec.rb'
}
it_behaves_like
'not finding a matching test file'
end
context
'with foss_test_only: true'
do
let
(
:file
)
{
'ee/app/models/ee/user.rb'
}
let
(
:test_files
)
{
[
'spec/app/models/user_spec.rb'
]
}
subject
{
Quality
::
TestFileFinder
.
new
(
file
,
foss_test_only:
true
)
}
it
'excludes matching ee test files'
do
expect
(
subject
.
test_files
).
to
match_array
(
test_files
)
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment