Skip to content

Commit

Permalink
feat: Dangerfile test exclusions for any line
Browse files Browse the repository at this point in the history
  • Loading branch information
bryankaraffa authored Dec 9, 2024
1 parent a547be4 commit 7fad16b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 14 deletions.
2 changes: 0 additions & 2 deletions .dangerfile/policy_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1465,8 +1465,6 @@ def policy_console_log?(file, file_lines)

file_lines.each_with_index do |line, index|
line_number = index + 1
# Exclude the line if it contains the specific phrase `// Excluded from console.log test.*`
next if line.include?("// Excluded from console.log test")
fail_message += "Line #{line_number.to_s}\n" if line.include?("console.log")
end

Expand Down
10 changes: 10 additions & 0 deletions Dangerfile
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,16 @@ changed_pt_files.each do |file|
file_lines = File.readlines(file)
file_diff = git.diff_for_file(file)

# Allow policy developer to exclude certain lines from testing
# This should be done with caution, as it can hide potential issues/noncompliance
# Exclusion can be used to exclude lines that are known to be false positives (or approved exceptions)
# Exclusion should be done in the PT file itself, and should be documented in the file
# Exclusion should be done in the following format:
# `// Dangerfile ignore: <reason for exclusion>`
# OR
# `# Dangerfile ignore: <reason for exclusion>`
file_lines = file_lines.reject { |line| line.contains('// Dangerfile ignore') || line.contains('# Dangerfile ignore') }

# Raise error if policy is deprecated but missing deprecated field in info() block
test = policy_missing_deprecated_field?(file, file_parsed); failures << test if test

Expand Down
8 changes: 4 additions & 4 deletions cost/aws/schedule_instance/aws_schedule_instance.pt
Original file line number Diff line number Diff line change
Expand Up @@ -510,11 +510,11 @@ script "js_instances_with_schedule", type: "javascript" do
// Check that we have a schedule and it is not explicitely disabled before attempting to parse the schedule tag value
// Test the regex pattern for a valid schedule as well, in case the tag value is not a valid schedule
// schedule="08:00-17:30;MO,TU,WE,TH,FR"
// schedule="08:00-17:30;MO,TU,WE,TH,FR;America/Los_Angeles"
var schedule_is_valid = /([0-9]{1,2}.*)-([0-9]{1,2}.*);([A-Za-z,]*).*/.test(schedule)
// schedule="08:00-17:30;MO,TU,WE,TH,FR" // Dangerfile ignore: example triggering false positive
// schedule="08:00-17:30;MO,TU,WE,TH,FR;America/Los_Angeles" // Dangerfile ignore: example triggering false positive
var schedule_is_valid = /([0-9]{1,2}.*)-([0-9]{1,2}.*);([A-Za-z,]*).*/.test(schedule) // Dangerfile ignore: regex triggering false positive
if ((_.isString(schedule)) && (schedule_is_valid == false)) {
console.log("Skipping instance " + instance['instanceId'] + " with invalid schedule '" + schedule +"'"); // Excluded from console.log test
console.log("Skipping instance " + instance['instanceId'] + " with invalid schedule '" + schedule +"'"); // Dangerfile ignore: allowed console.log output
} else if ((_.isString(schedule)) && (schedule_is_valid)) {
var schedule_split = schedule.split(';')
Expand Down
8 changes: 4 additions & 4 deletions cost/azure/schedule_instance/azure_schedule_instance.pt
Original file line number Diff line number Diff line change
Expand Up @@ -548,11 +548,11 @@ script "js_instances_with_schedule", type: "javascript" do
// Check that we have a schedule and it is not explicitely disabled before attempting to parse the schedule tag value
// Test the regex pattern for a valid schedule as well, in case the tag value is not a valid schedule
// schedule="08:00-17:30;MO,TU,WE,TH,FR"
// schedule="08:00-17:30;MO,TU,WE,TH,FR;America/Los_Angeles"
var schedule_is_valid = /([0-9]{1,2}.*)-([0-9]{1,2}.*);([A-Za-z,]*).*/.test(schedule)
// schedule="08:00-17:30;MO,TU,WE,TH,FR" // Dangerfile ignore: example triggering false positive
// schedule="08:00-17:30;MO,TU,WE,TH,FR;America/Los_Angeles" // Dangerfile ignore: example triggering false positive
var schedule_is_valid = /([0-9]{1,2}.*)-([0-9]{1,2}.*);([A-Za-z,]*).*/.test(schedule) // Dangerfile ignore: regex triggering false positive
if ((_.isString(schedule)) && (schedule_is_valid == false)) {
console.log("Skipping instance " + instance['resourceID'] + " with invalid schedule '" + schedule +"'"); // Excluded from console.log test
console.log("Skipping instance " + instance['resourceID'] + " with invalid schedule '" + schedule +"'"); // Dangerfile ignore: allowed console.log output
} else if ((_.isString(schedule)) && (schedule_is_valid)) {
var schedule_split = schedule.split(';')
// Parse Time - first part of schedule
Expand Down
8 changes: 4 additions & 4 deletions cost/google/schedule_instance/google_schedule_instance.pt
Original file line number Diff line number Diff line change
Expand Up @@ -493,11 +493,11 @@ script "js_instances_with_schedule", type: "javascript" do
// Check that we have a schedule and it is not explicitely disabled before attempting to parse the schedule tag value
// Test the regex pattern for a valid schedule as well, in case the tag value is not a valid schedule
// schedule="0900-0930_mo-tu-we-th-fr"
// schedule="0900-0930_mo-tu-we-th-fr_america-los_angeles"
var schedule_is_valid = /([0-9]{1,2}.*)-([0-9]{1,2}.*)_([a-z\-]*).*/.test(schedule)
// schedule="0900-0930_mo-tu-we-th-fr" // Dangerfile ignore: example triggering false positive
// schedule="0900-0930_mo-tu-we-th-fr_america-los_angeles" // Dangerfile ignore: example triggering false positive
var schedule_is_valid = /([0-9]{1,2}.*)-([0-9]{1,2}.*)_([a-z\-]*).*/.test(schedule) // Dangerfile ignore: regex triggering false positive
if ((_.isString(schedule)) && (schedule_is_valid == false)) {
console.log("Skipping instance " + instance['instanceId'] + " with invalid schedule '" + schedule +"'"); // Excluded from console.log test
console.log("Skipping instance " + instance['instanceId'] + " with invalid schedule '" + schedule +"'"); // Dangerfile ignore: allowed console.log output
} else if ((_.isString(schedule)) && (schedule_is_valid)) {
var schedule_split = schedule.split('_')
Expand Down

0 comments on commit 7fad16b

Please sign in to comment.