Skip to content

Commit

Permalink
Update validator to check for cache only for specific TEST_SUIT_ID
Browse files Browse the repository at this point in the history
  • Loading branch information
EdwinPhilip authored Mar 11, 2024
1 parent 568c5e7 commit 868b6d9
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions lib/rspec_tracer/remote_cache/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,24 @@ class Validator
def initialize
@test_suite_id = ENV['TEST_SUITE_ID']
@test_suites = ENV['TEST_SUITES']
@use_test_suite_id_cache = ENV['USE_TEST_SUITE_ID_CACHE']

if @test_suite_id.nil? ^ @test_suites.nil?
raise(
ValidationError,
'Both the enviornment variables TEST_SUITE_ID and TEST_SUITES are not set'
'Both the environment variables TEST_SUITE_ID and TEST_SUITES are not set'
)
end

setup
end

def valid?(ref, cache_files)
last_run_regex = Regexp.new(format(@last_run_files_regex, ref: ref))

return false if cache_files.count { |file| file.match?(last_run_regex) } != @last_run_files_count

cache_regex = Regexp.new(format(@cached_files_regex, ref: ref))

cache_files.count { |file| file.match?(cache_regex) } == @cached_files_count
if @use_test_suite_id_cache
test_suite_id_specific_validation(ref, cache_files)
else
general_validation(ref, cache_files)
end
end

private
Expand All @@ -36,7 +35,7 @@ def setup
@last_run_files_count = 1
@last_run_files_regex = '/%<ref>s/last_run.json$'
@cached_files_count = CACHE_FILES_PER_TEST_SUITE
@cached_files_regex = '/%<ref>s/[0-9a-f]{32}/.+.json'
@cached_files_regex = '/%<ref>s/[0-9a-f]{32}/.+.json$'
else
@test_suites = @test_suites.to_i
@test_suites_regex = (1..@test_suites).to_a.join('|')
Expand All @@ -47,6 +46,29 @@ def setup
@cached_files_regex = "/%<ref>s/(#{@test_suites_regex})/[0-9a-f]{32}/.+.json$"
end
end

def general_validation(ref, cache_files)
last_run_regex = Regexp.new(format(@last_run_files_regex, ref: ref))

return false if cache_files.count { |file| file.match?(last_run_regex) } != @last_run_files_count

cache_regex = Regexp.new(format(@cached_files_regex, ref: ref))

cache_files.count { |file| file.match?(cache_regex) } == @cached_files_count
end

def test_suite_id_specific_validation(ref, cache_files)
# Here, we ensure that the regex is dynamically adjusted for the specific test suite
# Adjusting for specific test_suite_id in the regex patterns
last_run_regex = Regexp.new("/#{ref}/#{@test_suite_id}/last_run.json$")
cache_regex = Regexp.new("/#{ref}/#{@test_suite_id}/[0-9a-f]{32}/.+.json$")

# Validate presence of the last run file for the specific test suite
return false unless cache_files.any? { |file| file.match?(last_run_regex) }

# Check if any cache files for the specific test suite are present
cache_files.any? { |file| file.match?(cache_regex) }
end
end
end
end

0 comments on commit 868b6d9

Please sign in to comment.