Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update validator to check for cache only for specific TEST_SUIT_ID #70

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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']
EdwinPhilip marked this conversation as resolved.
Show resolved Hide resolved

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
Loading