Skip to content

Commit 868b6d9

Browse files
authored
Update validator to check for cache only for specific TEST_SUIT_ID
1 parent 568c5e7 commit 868b6d9

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

lib/rspec_tracer/remote_cache/validator.rb

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,24 @@ class Validator
88
def initialize
99
@test_suite_id = ENV['TEST_SUITE_ID']
1010
@test_suites = ENV['TEST_SUITES']
11+
@use_test_suite_id_cache = ENV['USE_TEST_SUITE_ID_CACHE']
1112

1213
if @test_suite_id.nil? ^ @test_suites.nil?
1314
raise(
1415
ValidationError,
15-
'Both the enviornment variables TEST_SUITE_ID and TEST_SUITES are not set'
16+
'Both the environment variables TEST_SUITE_ID and TEST_SUITES are not set'
1617
)
1718
end
1819

1920
setup
2021
end
2122

2223
def valid?(ref, cache_files)
23-
last_run_regex = Regexp.new(format(@last_run_files_regex, ref: ref))
24-
25-
return false if cache_files.count { |file| file.match?(last_run_regex) } != @last_run_files_count
26-
27-
cache_regex = Regexp.new(format(@cached_files_regex, ref: ref))
28-
29-
cache_files.count { |file| file.match?(cache_regex) } == @cached_files_count
24+
if @use_test_suite_id_cache
25+
test_suite_id_specific_validation(ref, cache_files)
26+
else
27+
general_validation(ref, cache_files)
28+
end
3029
end
3130

3231
private
@@ -36,7 +35,7 @@ def setup
3635
@last_run_files_count = 1
3736
@last_run_files_regex = '/%<ref>s/last_run.json$'
3837
@cached_files_count = CACHE_FILES_PER_TEST_SUITE
39-
@cached_files_regex = '/%<ref>s/[0-9a-f]{32}/.+.json'
38+
@cached_files_regex = '/%<ref>s/[0-9a-f]{32}/.+.json$'
4039
else
4140
@test_suites = @test_suites.to_i
4241
@test_suites_regex = (1..@test_suites).to_a.join('|')
@@ -47,6 +46,29 @@ def setup
4746
@cached_files_regex = "/%<ref>s/(#{@test_suites_regex})/[0-9a-f]{32}/.+.json$"
4847
end
4948
end
49+
50+
def general_validation(ref, cache_files)
51+
last_run_regex = Regexp.new(format(@last_run_files_regex, ref: ref))
52+
53+
return false if cache_files.count { |file| file.match?(last_run_regex) } != @last_run_files_count
54+
55+
cache_regex = Regexp.new(format(@cached_files_regex, ref: ref))
56+
57+
cache_files.count { |file| file.match?(cache_regex) } == @cached_files_count
58+
end
59+
60+
def test_suite_id_specific_validation(ref, cache_files)
61+
# Here, we ensure that the regex is dynamically adjusted for the specific test suite
62+
# Adjusting for specific test_suite_id in the regex patterns
63+
last_run_regex = Regexp.new("/#{ref}/#{@test_suite_id}/last_run.json$")
64+
cache_regex = Regexp.new("/#{ref}/#{@test_suite_id}/[0-9a-f]{32}/.+.json$")
65+
66+
# Validate presence of the last run file for the specific test suite
67+
return false unless cache_files.any? { |file| file.match?(last_run_regex) }
68+
69+
# Check if any cache files for the specific test suite are present
70+
cache_files.any? { |file| file.match?(cache_regex) }
71+
end
5072
end
5173
end
5274
end

0 commit comments

Comments
 (0)