/
testutil.go
83 lines (71 loc) · 1.28 KB
/
testutil.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// SPDX-FileCopyrightText: 2022 Sascha Brawer <sascha@brawer.ch>
// SPDX-License-Identifier: MIT
package main
import (
"compress/gzip"
"io"
"os"
"github.com/andybalholm/brotli"
)
func readBrotliFile(path string) string {
f, err := os.Open(path)
if err != nil {
panic(err)
}
defer f.Close()
reader := brotli.NewReader(f)
b, err := io.ReadAll(reader)
if err != nil {
panic(err)
}
return string(b)
}
func readGzipFile(path string) string {
f, err := os.Open(path)
if err != nil {
panic(err)
}
defer f.Close()
reader, err := gzip.NewReader(f)
if err != nil {
panic(err)
}
b, err := io.ReadAll(reader)
if err != nil {
panic(err)
}
return string(b)
}
func writeBrotli(path string, content string) {
f, err := os.Create(path)
if err != nil {
panic(err)
}
defer f.Close()
s := brotli.NewWriterLevel(f, 1)
s.Write([]byte(content))
if err := s.Close(); err != nil {
panic(err)
}
if err := f.Close(); err != nil {
panic(err)
}
}
func writeGzipFile(path string, content string) {
f, err := os.Create(path)
if err != nil {
panic(err)
}
defer f.Close()
s, err := gzip.NewWriterLevel(f, 1)
if err != nil {
panic(err)
}
s.Write([]byte(content))
if err := s.Close(); err != nil {
panic(err)
}
if err := f.Close(); err != nil {
panic(err)
}
}