|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "test_helper" |
| 4 | +require "tempfile" |
| 5 | +require "stringio" |
| 6 | +require "zlib" |
| 7 | + |
| 8 | +class TestResult < Minitest::Test |
| 9 | + include FirefoxTestHelpers |
| 10 | + include CpuprofileTestHelpers |
| 11 | + |
| 12 | + def setup |
| 13 | + @result = Vernier.trace do |
| 14 | + sleep 0.001 |
| 15 | + end |
| 16 | + end |
| 17 | + |
| 18 | + def test_write_to_file_firefox_format |
| 19 | + Tempfile.open("vernier_test") do |tempfile| |
| 20 | + @result.write(out: tempfile.path, format: "firefox") |
| 21 | + tempfile.rewind |
| 22 | + content = tempfile.read |
| 23 | + assert_valid_firefox_profile(content) |
| 24 | + end |
| 25 | + end |
| 26 | + |
| 27 | + def test_write_to_file_cpuprofile_format |
| 28 | + Tempfile.open("vernier_test") do |tempfile| |
| 29 | + @result.write(out: tempfile.path, format: "cpuprofile") |
| 30 | + tempfile.rewind |
| 31 | + content = tempfile.read |
| 32 | + assert_valid_cpuprofile(content) |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + def test_write_to_stringio_firefox_format |
| 37 | + stringio = StringIO.new |
| 38 | + @result.write(out: stringio, format: "firefox") |
| 39 | + stringio.rewind |
| 40 | + content = stringio.read |
| 41 | + assert_valid_firefox_profile(content) |
| 42 | + end |
| 43 | + |
| 44 | + def test_write_to_stringio_cpuprofile_format |
| 45 | + stringio = StringIO.new |
| 46 | + @result.write(out: stringio, format: "cpuprofile") |
| 47 | + stringio.rewind |
| 48 | + content = stringio.read |
| 49 | + assert_valid_cpuprofile(content) |
| 50 | + end |
| 51 | + |
| 52 | + def test_write_to_file_with_gz_extension |
| 53 | + Tempfile.open(["vernier_test", ".gz"]) do |tempfile| |
| 54 | + @result.write(out: tempfile.path, format: "firefox") |
| 55 | + tempfile.rewind |
| 56 | + content = tempfile.read |
| 57 | + decompressed = Zlib.gunzip(content) |
| 58 | + assert_valid_firefox_profile(decompressed) |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + def test_write_to_stringio_ignores_gz_extension_logic |
| 63 | + stringio = StringIO.new |
| 64 | + @result.write(out: stringio, format: "firefox") |
| 65 | + stringio.rewind |
| 66 | + content = stringio.read |
| 67 | + assert_valid_firefox_profile(content) |
| 68 | + end |
| 69 | + |
| 70 | + def test_write_with_invalid_format_raises_error |
| 71 | + stringio = StringIO.new |
| 72 | + error = assert_raises(ArgumentError) do |
| 73 | + @result.write(out: stringio, format: "invalid") |
| 74 | + end |
| 75 | + assert_equal "unknown format: invalid", error.message |
| 76 | + end |
| 77 | +end |
0 commit comments