Skip to content
GitLab
菜单
为什么选择 GitLab
定价
联系销售
探索
为什么选择 GitLab
定价
联系销售
探索
登录
获取免费试用
主导航
搜索或转到…
项目
GitLab
管理
动态
成员
标记
计划
议题
议题看板
里程碑
迭代
需求
代码
合并请求
仓库
分支
提交
标签
仓库图
比较修订版本
代码片段
锁定的文件
构建
流水线
作业
流水线计划
测试用例
产物
部署
发布
Package registry
Container registry
模型注册表
运维
环境
Terraform 模块
监控
事件
服务台
分析
价值流分析
贡献者分析
CI/CD 分析
仓库分析
代码评审分析
议题分析
洞察
模型实验
效能分析
帮助
帮助
支持
GitLab 文档
比较 GitLab 各版本
社区论坛
为极狐GitLab 提交贡献
提交反馈
隐私声明
快捷键
?
新增功能
4
代码片段
群组
项目
显示更多面包屑
gitlab-cn
GitLab
提交
967a29f9
提交
967a29f9
编辑于
2 years ago
作者:
David Dieulivol
提交者:
Rémy Coutable
2 years ago
浏览文件
操作
下载
补丁
差异文件
Extract find_tests logic to ruby class + add specs
上级
2d9540e6
No related branches found
分支 包含提交
No related tags found
标签 包含提交
无相关合并请求
变更
3
隐藏空白变更内容
行内
左右并排
显示
3 个更改的文件
spec/tooling/lib/tooling/find_tests_spec.rb
+155
-0
155 个添加, 0 个删除
spec/tooling/lib/tooling/find_tests_spec.rb
tooling/bin/find_tests
+4
-14
4 个添加, 14 个删除
tooling/bin/find_tests
tooling/lib/tooling/find_tests.rb
+33
-0
33 个添加, 0 个删除
tooling/lib/tooling/find_tests.rb
有
192 个添加
和
14 个删除
spec/tooling/lib/tooling/find_tests_spec.rb
0 → 100644
+
155
−
0
浏览文件 @
967a29f9
# frozen_string_literal: true
require
'tempfile'
require_relative
'../../../../tooling/lib/tooling/find_tests'
require_relative
'../../../support/helpers/stub_env'
RSpec
.
describe
Tooling
::
FindTests
,
feature_category: :tooling
do
include
StubENV
attr_accessor
:changes_file
,
:matching_tests_paths
let
(
:instance
)
{
described_class
.
new
(
changes_file
,
matching_tests_paths
)
}
let
(
:mock_test_file_finder
)
{
instance_double
(
TestFileFinder
::
FileFinder
)
}
let
(
:new_matching_tests
)
{
[
"new_matching_spec.rb"
]
}
let
(
:changes_file_content
)
{
"changed_file1 changed_file2"
}
let
(
:matching_tests_paths_content
)
{
"previously_matching_spec.rb"
}
around
do
|
example
|
self
.
changes_file
=
Tempfile
.
new
(
'changes'
)
self
.
matching_tests_paths
=
Tempfile
.
new
(
'matching_tests'
)
# See https://ruby-doc.org/stdlib-1.9.3/libdoc/tempfile/rdoc/
# Tempfile.html#class-Tempfile-label-Explicit+close
begin
example
.
run
ensure
changes_file
.
close
matching_tests_paths
.
close
changes_file
.
unlink
matching_tests_paths
.
unlink
end
end
before
do
allow
(
mock_test_file_finder
).
to
receive
(
:use
)
allow
(
mock_test_file_finder
).
to
receive
(
:test_files
).
and_return
(
new_matching_tests
)
allow
(
TestFileFinder
::
FileFinder
).
to
receive
(
:new
).
and_return
(
mock_test_file_finder
)
stub_env
(
'RSPEC_TESTS_MAPPING_ENABLED'
=>
nil
,
'RSPEC_TESTS_MAPPING_PATH'
=>
'/tmp/does-not-exist.out'
)
# We write into the temp files initially, to later check how the code modified those files
File
.
write
(
changes_file
,
changes_file_content
)
File
.
write
(
matching_tests_paths
,
matching_tests_paths_content
)
end
describe
'#execute'
do
subject
{
instance
.
execute
}
context
'when the matching_tests_paths file does not exist'
do
before
do
allow
(
File
).
to
receive
(
:exist?
).
and_return
(
false
)
allow
(
File
).
to
receive
(
:write
).
with
(
matching_tests_paths
,
any_args
)
end
it
'creates an empty file'
do
expect
(
File
).
to
receive
(
:write
).
with
(
matching_tests_paths
,
''
)
subject
end
end
context
'when the matching_tests_paths file already exists'
do
it
'does not create an empty file'
do
expect
(
File
).
not_to
receive
(
:write
).
with
(
matching_tests_paths
,
''
)
subject
end
end
it
'does not modify the content of the input file'
do
expect
{
subject
}.
not_to
change
{
File
.
read
(
changes_file
)
}
end
it
'does not overwrite the output file'
do
expect
{
subject
}.
to
change
{
File
.
read
(
matching_tests_paths
)
}
.
from
(
matching_tests_paths_content
)
.
to
(
"
#{
matching_tests_paths_content
}
#{
new_matching_tests
.
uniq
.
join
(
' '
)
}
"
)
end
it
'loads the tests.yml file with a pattern matching mapping'
do
expect
(
TestFileFinder
::
MappingStrategies
::
PatternMatching
).
to
receive
(
:load
).
with
(
'tests.yml'
)
subject
end
context
'when RSPEC_TESTS_MAPPING_ENABLED env variable is set'
do
before
do
stub_env
(
'RSPEC_TESTS_MAPPING_ENABLED'
=>
'true'
,
'RSPEC_TESTS_MAPPING_PATH'
=>
'crystalball-test/mapping.json'
)
end
it
'loads the direct matching pattern file'
do
expect
(
TestFileFinder
::
MappingStrategies
::
DirectMatching
)
.
to
receive
(
:load_json
)
.
with
(
'crystalball-test/mapping.json'
)
subject
end
end
context
'when RSPEC_TESTS_MAPPING_ENABLED env variable is not set'
do
let
(
:rspec_tests_mapping_enabled
)
{
''
}
before
do
stub_env
(
'RSPEC_TESTS_MAPPING_ENABLED'
=>
rspec_tests_mapping_enabled
,
'RSPEC_TESTS_MAPPING_PATH'
=>
rspec_tests_mapping_path
)
end
context
'when RSPEC_TESTS_MAPPING_PATH is set'
do
let
(
:rspec_tests_mapping_path
)
{
'crystalball-test/mapping.json'
}
it
'does not load the direct matching pattern file'
do
expect
(
TestFileFinder
::
MappingStrategies
::
DirectMatching
).
not_to
receive
(
:load_json
)
subject
end
end
context
'when RSPEC_TESTS_MAPPING_PATH is not set'
do
let
(
:rspec_tests_mapping_path
)
{
nil
}
it
'does not load the direct matching pattern file'
do
expect
(
TestFileFinder
::
MappingStrategies
::
DirectMatching
).
not_to
receive
(
:load_json
)
subject
end
end
end
context
'when the same spec is matching multiple times'
do
let
(
:new_matching_tests
)
do
[
"new_matching_spec.rb"
,
"duplicate_spec.rb"
,
"duplicate_spec.rb"
]
end
it
'writes uniquely matching specs to the output'
do
subject
expect
(
File
.
read
(
matching_tests_paths
).
split
(
' '
)).
to
match_array
(
matching_tests_paths_content
.
split
(
' '
)
+
new_matching_tests
.
uniq
)
end
end
end
end
此差异已折叠。
点击以展开。
tooling/bin/find_tests
+
4
−
14
浏览文件 @
967a29f9
#!/usr/bin/env ruby
# frozen_string_literal: true
require
'test_file_finder
'
require
_relative
'../lib/tooling/find_tests
'
changes
=
ARGV
.
shift
output_file
=
ARGV
.
shift
changes
_file
=
ARGV
.
shift
matching_tests_paths
=
ARGV
.
shift
changed_files
=
File
.
read
(
changes
).
split
(
' '
)
tff
=
TestFileFinder
::
FileFinder
.
new
(
paths:
changed_files
).
tap
do
|
file_finder
|
file_finder
.
use
TestFileFinder
::
MappingStrategies
::
PatternMatching
.
load
(
'tests.yml'
)
if
ENV
[
'RSPEC_TESTS_MAPPING_ENABLED'
]
file_finder
.
use
TestFileFinder
::
MappingStrategies
::
DirectMatching
.
load_json
(
ENV
[
'RSPEC_TESTS_MAPPING_PATH'
])
end
end
File
.
write
(
output_file
,
tff
.
test_files
.
uniq
.
join
(
' '
))
Tooling
::
FindTests
.
new
(
changes_file
,
matching_tests_paths
).
execute
此差异已折叠。
点击以展开。
tooling/lib/tooling/find_tests.rb
0 → 100644
+
33
−
0
浏览文件 @
967a29f9
# frozen_string_literal: true
require
'test_file_finder'
module
Tooling
class
FindTests
def
initialize
(
changes_file
,
matching_tests_paths
)
@matching_tests_paths
=
matching_tests_paths
@changed_files
=
File
.
read
(
changes_file
).
split
(
' '
)
File
.
write
(
matching_tests_paths
,
''
)
unless
File
.
exist?
(
matching_tests_paths
)
@matching_tests
=
File
.
read
(
matching_tests_paths
).
split
(
' '
)
end
def
execute
tff
=
TestFileFinder
::
FileFinder
.
new
(
paths:
changed_files
).
tap
do
|
file_finder
|
file_finder
.
use
TestFileFinder
::
MappingStrategies
::
PatternMatching
.
load
(
'tests.yml'
)
if
ENV
[
'RSPEC_TESTS_MAPPING_ENABLED'
]
==
'true'
file_finder
.
use
TestFileFinder
::
MappingStrategies
::
DirectMatching
.
load_json
(
ENV
[
'RSPEC_TESTS_MAPPING_PATH'
])
end
end
new_matching_tests
=
tff
.
test_files
.
uniq
File
.
write
(
matching_tests_paths
,
(
matching_tests
+
new_matching_tests
).
join
(
' '
))
end
private
attr_reader
:changed_files
,
:matching_tests
,
:matching_tests_paths
end
end
此差异已折叠。
点击以展开。
预览
0%
加载中
请重试
或
添加新附件
.
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
保存评论
取消
想要评论请
注册
或
登录