Skip to content

Instantly share code, notes, and snippets.

@descentintomael
Created June 10, 2016 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save descentintomael/5dd5c08db1fa963d22b61591a2c73ed5 to your computer and use it in GitHub Desktop.
Save descentintomael/5dd5c08db1fa963d22b61591a2c73ed5 to your computer and use it in GitHub Desktop.
Some test helpers to make sure something is or isn't logged
module LogTestHelpers
def exclude_from_logs(*hits, &block)
exclude_from_other_logs(test_log_path, *hits, &block)
end
alias :expect_logs_to_not_include :exclude_from_logs
def exclude_from_other_logs(log_path=test_log_path, *hits, &block)
lines_to_scan = scan_logs log_path, &block
regex = Regexp.union *hits
expect(lines_to_scan.join("\n")).to_not match regex
end
def include_in_logs(*hits, &block)
include_in_other_logs(test_log_path, *hits, &block)
end
alias :expect_logs_to_include :include_in_logs
def include_in_other_logs(log_path=test_log_path, *hits, &block)
lines_to_scan = scan_logs log_path, &block
regex = Regexp.union *hits
expect(lines_to_scan.join("\n")).to match regex
end
def scan_logs(log_path =test_log_path)
log_starting_point = File.size(log_path)
yield # This is where we run the actual code that is writing to the log
File.read(log_path, nil, log_starting_point, {mode: 'r'}).lines
end
def test_log_path
"log/test.log"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment