Skip to content

Commit 612cdb0

Browse files
authored
Merge pull request #164 from jhawthorn/support-writing-result-to-stringio
2 parents 59317c1 + 236cf01 commit 612cdb0

File tree

2 files changed

+90
-4
lines changed

2 files changed

+90
-4
lines changed

lib/vernier/result.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module Vernier
24
class Result
35
attr_accessor :stack_table
@@ -41,10 +43,17 @@ def to_cpuprofile
4143
def write(out:, format: "firefox")
4244
case format
4345
when "cpuprofile"
44-
File.binwrite(out, to_cpuprofile)
45-
when nil, "firefox"
46-
gzip = out.end_with?(".gz")
47-
File.binwrite(out, to_firefox(gzip:))
46+
if out.respond_to?(:write)
47+
out.write(to_cpuprofile)
48+
else
49+
File.binwrite(out, to_cpuprofile)
50+
end
51+
when "firefox", nil
52+
if out.respond_to?(:write)
53+
out.write(to_firefox)
54+
else
55+
File.binwrite(out, to_firefox(gzip: out.end_with?(".gz")))
56+
end
4857
else
4958
raise ArgumentError, "unknown format: #{format}"
5059
end

test/test_result.rb

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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

Comments
 (0)