Skip to content

Commit 8f9953e

Browse files
authored
Copy testing utilities into separate package (#235)
1 parent 466b6d0 commit 8f9953e

File tree

9 files changed

+230
-15
lines changed

9 files changed

+230
-15
lines changed

.github/workflows/CI.yml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,25 @@ jobs:
3939
with:
4040
version: ${{ matrix.version }}
4141
arch: ${{ matrix.arch }}
42+
show-versioninfo: true
4243
- uses: julia-actions/cache@v2
43-
- uses: julia-actions/julia-buildpkg@v1
44-
- uses: julia-actions/julia-runtest@v1
44+
- name: Develop packages
45+
run: |
46+
using Pkg
47+
Pkg.Registry.update()
48+
Pkg.develop([
49+
PackageSpec(path="."),
50+
PackageSpec(path="./lib/TestsForCodecPackages"),
51+
])
52+
Pkg.update()
53+
Pkg.status(;mode=Pkg.PKGMODE_PROJECT)
54+
Pkg.status(;mode=Pkg.PKGMODE_MANIFEST)
55+
shell: julia --project=test --color=yes --check-bounds=yes {0}
56+
- name: Run tests
57+
env:
58+
JULIA_LOAD_PATH: "@"
59+
run: |
60+
julia --project=test --color=yes --depwarn=yes --warn-overwrite=yes --check-bounds=yes --startup-file=no --code-coverage=user test/runtests.jl
4561
- uses: julia-actions/julia-processcoverage@v1
4662
- uses: codecov/codecov-action@v4
4763
with:

.github/workflows/Downstream.yml

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,31 @@ jobs:
3737
with:
3838
version: ${{ matrix.julia-version }}
3939
arch: x64
40-
- uses: julia-actions/julia-buildpkg@latest
4140
- name: Clone Downstream
4241
uses: actions/checkout@v4
4342
with:
4443
repository: ${{ matrix.package.user }}/${{ matrix.package.repo }}
4544
path: downstream
4645
- name: Load this and run the downstream tests
47-
shell: julia --color=yes --project=downstream {0}
46+
shell: julia --color=yes {0}
4847
run: |
4948
using Pkg
49+
using TOML
50+
Pkg.Registry.update()
51+
Pkg.activate(;temp=true)
5052
try
51-
# force it to use this PR's version of the package
52-
Pkg.develop(PackageSpec(path=".")) # resolver may fail with main deps
53+
# force it to use this PR's version of the package and test package
54+
Pkg.develop([
55+
PackageSpec(path="downstream"),
56+
PackageSpec(path="."),
57+
PackageSpec(path="./lib/TestsForCodecPackages"),
58+
])
59+
# resolver may fail with main deps
5360
Pkg.update()
54-
Pkg.test() # resolver may fail with test time deps
61+
p1 = joinpath("downstream", "JuliaProject.toml")
62+
p2 = joinpath("downstream", "Project.toml")
63+
proj_toml = isfile(p1) ? p1 : p2
64+
Pkg.test(TOML.parsefile(proj_toml)["name"]) # resolver may fail with test time deps
5565
catch err
5666
err isa Pkg.Resolve.ResolverError || rethrow()
5767
# If we can't resolve that means this is incompatible by SemVer and this is fine.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
/docs/Manifest.toml
1414
/fuzz/test/
1515
/fuzz/Manifest.toml
16+
/lib/TestsForCodecPackages/Manifest.toml
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name = "TestsForCodecPackages"
2+
uuid = "c2e61002-3542-480d-8b3c-5f05cc4f8554"
3+
authors = ["nhz2 <[email protected]>"]
4+
version = "0.1.0"
5+
6+
[deps]
7+
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
8+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
9+
TranscodingStreams = "3bb67fe8-82b1-5028-8e26-92a6c54297fa"
10+
11+
[compat]
12+
Random = "1"
13+
Test = "1"
14+
TranscodingStreams = "0.11"
15+
julia = "1.6"
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
module TestsForCodecPackages
2+
3+
using Test: Test
4+
using Random: seed!, randstring
5+
6+
using TranscodingStreams: TranscodingStreams, initialize, finalize, transcode,
7+
TranscodingStream, NoopStream, TOKEN_END
8+
9+
TEST_RANDOM_SEED = 12345
10+
11+
export
12+
test_roundtrip_read,
13+
test_roundtrip_write,
14+
test_roundtrip_transcode,
15+
test_roundtrip_lines,
16+
test_roundtrip_seekstart,
17+
test_roundtrip_fileio,
18+
test_chunked_read,
19+
test_chunked_write
20+
21+
function test_roundtrip_read(encoder, decoder)
22+
seed!(TEST_RANDOM_SEED)
23+
for n in vcat(0:30, sort!(rand(500:100_000, 30))), alpha in (0x00:0xff, 0x00:0x0f)
24+
data = rand(alpha, n)
25+
file = IOBuffer(data)
26+
stream = decoder(encoder(file))
27+
Test.@test hash(read(stream)) == hash(data)
28+
close(stream)
29+
end
30+
end
31+
32+
function test_roundtrip_write(encoder, decoder)
33+
seed!(TEST_RANDOM_SEED)
34+
for n in vcat(0:30, sort!(rand(500:100_000, 30))), alpha in (0x00:0xff, 0x00:0x0f)
35+
data = rand(alpha, n)
36+
sink = IOBuffer()
37+
decode_sink = decoder(sink)
38+
stream = encoder(decode_sink)
39+
write(stream, data)
40+
write(stream, TranscodingStreams.TOKEN_END)
41+
flush(stream)
42+
write(decode_sink, TranscodingStreams.TOKEN_END)
43+
flush(decode_sink)
44+
Test.@test take!(sink) == data
45+
close(stream)
46+
end
47+
end
48+
49+
function test_roundtrip_transcode(encode, decode)
50+
seed!(TEST_RANDOM_SEED)
51+
encoder = encode()
52+
initialize(encoder)
53+
decoder = decode()
54+
initialize(decoder)
55+
for n in vcat(0:30, sort!(rand(500:100_000, 30))), alpha in (0x00:0xff, 0x00:0x0f)
56+
data = rand(alpha, n)
57+
Test.@test hash(transcode(decode, transcode(encode, data))) == hash(data)
58+
Test.@test hash(transcode(decoder, transcode(encoder, data))) == hash(data)
59+
end
60+
finalize(encoder)
61+
finalize(decoder)
62+
end
63+
64+
function test_roundtrip_lines(encoder, decoder)
65+
seed!(TEST_RANDOM_SEED)
66+
lines = String[]
67+
buf = IOBuffer()
68+
stream = encoder(buf)
69+
for i in 1:100_000
70+
line = randstring(rand(0:1000))
71+
println(stream, line)
72+
push!(lines, line)
73+
end
74+
write(stream, TOKEN_END)
75+
flush(stream)
76+
seekstart(buf)
77+
Test.@test hash(lines) == hash(readlines(decoder(buf)))
78+
end
79+
80+
function test_roundtrip_seekstart(encoder, decoder)
81+
seed!(TEST_RANDOM_SEED)
82+
for n in vcat(0:30, sort!(rand(500:100_000, 30))), alpha in (0x00:0xff, 0x00:0x0f)
83+
data = rand(alpha, n)
84+
file = IOBuffer(data)
85+
stream = decoder(encoder(file))
86+
for m in vcat(0:min(n,20), rand(0:n, 10))
87+
Test.@test read(stream, m) == @view(data[1:m])
88+
seekstart(stream)
89+
end
90+
seekstart(stream)
91+
Test.@test read(stream) == data
92+
seekstart(stream)
93+
Test.@test read(stream) == data
94+
close(stream)
95+
end
96+
end
97+
98+
function test_roundtrip_fileio(Encoder, Decoder)
99+
data = b"""
100+
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla sit amet tempus felis. Etiam molestie urna placerat iaculis pellentesque. Maecenas porttitor et dolor vitae posuere. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc eget nibh quam. Nullam aliquet interdum fringilla. Duis facilisis, lectus in consectetur varius, lorem sem tempor diam, nec auctor tellus nibh sit amet sapien. In ex nunc, elementum eget facilisis ut, luctus eu orci. Sed sapien urna, accumsan et elit non, auctor pretium massa. Phasellus consectetur nisi suscipit blandit aliquam. Nulla facilisi. Mauris pellentesque sem sit amet mi vestibulum eleifend. Nulla faucibus orci ac lorem efficitur, et blandit orci interdum. Aenean posuere ultrices ex sed rhoncus. Donec malesuada mollis sem, sed varius nunc sodales sed. Curabitur lobortis non justo non tristique.
101+
"""
102+
mktemp() do filename, file
103+
stream = TranscodingStream(Encoder(), file)
104+
write(stream, data)
105+
close(stream)
106+
stream = TranscodingStream(Decoder(), open(filename))
107+
Test.@test hash(read(stream)) == hash(data)
108+
close(stream)
109+
end
110+
end
111+
112+
function test_chunked_read(Encoder, Decoder)
113+
seed!(TEST_RANDOM_SEED)
114+
alpha = b"色即是空"
115+
encoder = Encoder()
116+
initialize(encoder)
117+
for sharedbuf in false:true
118+
for _ in 1:500
119+
chunks = [rand(alpha, rand(0:100)) for _ in 1:rand(1:100)]
120+
data = mapfoldl(x->transcode(encoder, x), vcat, chunks, init=UInt8[])
121+
buffer = NoopStream(IOBuffer(data))
122+
ok = true
123+
for chunk in chunks
124+
stream = TranscodingStream(Decoder(), buffer; stop_on_end=true, sharedbuf)
125+
ok &= read(stream) == chunk
126+
ok &= position(stream) == length(chunk)
127+
ok &= eof(stream)
128+
ok &= isreadable(stream)
129+
close(stream)
130+
end
131+
# read without stop_on_end should read the full data.
132+
stream = TranscodingStream(Decoder(), IOBuffer(data))
133+
ok &= read(stream) == reduce(vcat, chunks)
134+
close(stream)
135+
Test.@test ok
136+
end
137+
end
138+
finalize(encoder)
139+
end
140+
141+
function test_chunked_write(Encoder, Decoder)
142+
seed!(TEST_RANDOM_SEED)
143+
alpha = b"空即是色"
144+
encoder = Encoder()
145+
initialize(encoder)
146+
for _ in 1:500
147+
chunks = [rand(alpha, rand(0:100)) for _ in 1:2]
148+
data = map(x->transcode(encoder, x), chunks)
149+
buffer = IOBuffer()
150+
stream = TranscodingStream(Decoder(), buffer, stop_on_end=true)
151+
write(stream, vcat(data...))
152+
close(stream)
153+
ok = true
154+
ok &= hash(take!(buffer)) == hash(vcat(chunks...))
155+
Test.@test ok
156+
end
157+
finalize(encoder)
158+
end
159+
160+
end # module TestsForCodecPackages

test/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
44
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
55
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
66
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
7+
TestsForCodecPackages = "c2e61002-3542-480d-8b3c-5f05cc4f8554"
78
TranscodingStreams = "3bb67fe8-82b1-5028-8e26-92a6c54297fa"

test/codecdoubleframe.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ using Test
44
using TranscodingStreams:
55
TranscodingStreams,
66
TranscodingStream,
7+
Error
8+
using TestsForCodecPackages:
79
test_roundtrip_read,
810
test_roundtrip_write,
11+
test_roundtrip_transcode,
912
test_roundtrip_lines,
1013
test_roundtrip_seekstart,
11-
test_roundtrip_transcode,
1214
test_roundtrip_fileio,
1315
test_chunked_read,
14-
test_chunked_write,
15-
Error
16+
test_chunked_write
1617

1718
# An insane codec for testing the codec APIs.
1819
struct DoubleFrameEncoder <: TranscodingStreams.Codec

test/codecnoop.jl

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
using OffsetArrays: OffsetArray
22
using FillArrays: Zeros
3+
using TestsForCodecPackages:
4+
test_roundtrip_read,
5+
test_roundtrip_write,
6+
test_roundtrip_transcode,
7+
test_roundtrip_lines,
8+
test_roundtrip_seekstart,
9+
test_roundtrip_fileio,
10+
test_chunked_read,
11+
test_chunked_write
312

413
@testset "Noop Codec" begin
514
source = IOBuffer("")
@@ -294,11 +303,11 @@ using FillArrays: Zeros
294303
data = "foo"
295304
@test String(transcode(Noop, data)) == data
296305

297-
TranscodingStreams.test_roundtrip_transcode(Noop, Noop)
298-
TranscodingStreams.test_roundtrip_read(NoopStream, NoopStream)
299-
TranscodingStreams.test_roundtrip_write(NoopStream, NoopStream)
300-
TranscodingStreams.test_roundtrip_lines(NoopStream, NoopStream)
301-
TranscodingStreams.test_roundtrip_seekstart(NoopStream, NoopStream)
306+
test_roundtrip_transcode(Noop, Noop)
307+
test_roundtrip_read(NoopStream, NoopStream)
308+
test_roundtrip_write(NoopStream, NoopStream)
309+
test_roundtrip_lines(NoopStream, NoopStream)
310+
test_roundtrip_seekstart(NoopStream, NoopStream)
302311

303312
@testset "switch write => read" begin
304313
stream = NoopStream(IOBuffer(collect(b"foobar"), read=true, write=true))

test/runtests.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using TranscodingStreams
2+
using TestsForCodecPackages: TestsForCodecPackages
23
using Random
34
using Test
45
using Aqua: Aqua
56

67
Aqua.test_all(TranscodingStreams)
8+
Aqua.test_all(TestsForCodecPackages)
79

810
@test isempty(detect_unbound_args(TranscodingStreams; recursive=true))
911
@test isempty(detect_ambiguities(TranscodingStreams; recursive=true))

0 commit comments

Comments
 (0)