Skip to content

Commit e370eac

Browse files
authored
feat(testing): add unit tests for file package (#757)
This change introduces unit tests for the `file.go` file in the `internal/librarian` package. The tests cover the following functions: - `readAllBytesFromFile` - `appendToFile` - `createAndWriteToFile` - `createAndWriteBytesToFile` The tests use a table-driven approach to test multiple scenarios, including success, error, and edge cases. Fixes: #740
1 parent 0f1c88e commit e370eac

File tree

1 file changed

+185
-0
lines changed

1 file changed

+185
-0
lines changed

internal/librarian/file_test.go

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package librarian
16+
17+
import (
18+
"os"
19+
"path/filepath"
20+
"testing"
21+
22+
"github.com/google/go-cmp/cmp"
23+
)
24+
25+
func setupTestFile(t *testing.T, content []byte, createDir bool) string {
26+
t.Helper()
27+
tempDir := t.TempDir()
28+
filePath := filepath.Join(tempDir, "test.txt")
29+
if !createDir {
30+
filePath = filepath.Join(tempDir, "non-existent-dir", "test.txt")
31+
}
32+
if content != nil {
33+
if err := os.WriteFile(filePath, content, 0644); err != nil {
34+
t.Fatalf("WriteFile() error = %v", err)
35+
}
36+
}
37+
return filePath
38+
}
39+
40+
func TestReadAllBytesFromFile(t *testing.T) {
41+
t.Parallel()
42+
testCases := []struct {
43+
name string
44+
content []byte
45+
wantErr bool
46+
}{
47+
{"success", []byte("hello world"), false},
48+
{"empty file", []byte(""), false},
49+
{"non-existent file", nil, true},
50+
}
51+
52+
for _, tc := range testCases {
53+
tc := tc
54+
t.Run(tc.name, func(t *testing.T) {
55+
t.Parallel()
56+
filePath := setupTestFile(t, tc.content, true)
57+
58+
got, err := readAllBytesFromFile(filePath)
59+
if (err != nil) != tc.wantErr {
60+
t.Fatalf("readAllBytesFromFile() error = %v, wantErr %v", err, tc.wantErr)
61+
}
62+
63+
if !tc.wantErr {
64+
if diff := cmp.Diff(tc.content, got); diff != "" {
65+
t.Errorf("readAllBytesFromFile() mismatch (-want +got):\n%s", diff)
66+
}
67+
}
68+
})
69+
}
70+
}
71+
72+
func TestAppendToFile(t *testing.T) {
73+
t.Parallel()
74+
testCases := []struct {
75+
name string
76+
initialContent []byte
77+
appendedContent string
78+
expectedContent string
79+
createDir bool
80+
wantErr bool
81+
}{
82+
{"success", []byte("hello"), " world", "hello world", true, false},
83+
{"empty initial content", []byte(""), "hello", "hello", true, false},
84+
{"empty appended content", []byte("hello"), "", "hello", true, false},
85+
{"append to non-existent file", nil, "hello", "hello", true, false},
86+
{"non-existent dir", nil, "hello", "", false, true},
87+
}
88+
89+
for _, tc := range testCases {
90+
tc := tc
91+
t.Run(tc.name, func(t *testing.T) {
92+
t.Parallel()
93+
filePath := setupTestFile(t, tc.initialContent, tc.createDir)
94+
95+
err := appendToFile(filePath, tc.appendedContent)
96+
if (err != nil) != tc.wantErr {
97+
t.Fatalf("appendToFile() error = %v, wantErr %v", err, tc.wantErr)
98+
}
99+
100+
if !tc.wantErr {
101+
got, err := os.ReadFile(filePath)
102+
if err != nil {
103+
t.Fatalf("ReadFile() error = %v", err)
104+
}
105+
if string(got) != tc.expectedContent {
106+
t.Errorf("appendToFile() got %q, want %q", string(got), tc.expectedContent)
107+
}
108+
}
109+
})
110+
}
111+
}
112+
113+
func TestCreateAndWriteToFile(t *testing.T) {
114+
t.Parallel()
115+
testCases := []struct {
116+
name string
117+
content string
118+
createDir bool
119+
wantErr bool
120+
}{
121+
{"success", "hello world", true, false},
122+
{"empty content", "", true, false},
123+
{"non-existent dir", "hello", false, true},
124+
}
125+
126+
for _, tc := range testCases {
127+
tc := tc
128+
t.Run(tc.name, func(t *testing.T) {
129+
t.Parallel()
130+
filePath := setupTestFile(t, nil, tc.createDir)
131+
132+
err := createAndWriteToFile(filePath, tc.content)
133+
if (err != nil) != tc.wantErr {
134+
t.Fatalf("createAndWriteToFile() error = %v, wantErr %v", err, tc.wantErr)
135+
}
136+
137+
if !tc.wantErr {
138+
got, err := os.ReadFile(filePath)
139+
if err != nil {
140+
t.Fatalf("ReadFile() error = %v", err)
141+
}
142+
if string(got) != tc.content {
143+
t.Errorf("createAndWriteToFile() got %q, want %q", string(got), tc.content)
144+
}
145+
}
146+
})
147+
}
148+
}
149+
150+
func TestCreateAndWriteBytesToFile(t *testing.T) {
151+
t.Parallel()
152+
testCases := []struct {
153+
name string
154+
content []byte
155+
createDir bool
156+
wantErr bool
157+
}{
158+
{"success", []byte("hello world"), true, false},
159+
{"empty content", []byte(""), true, false},
160+
{"non-existent dir", []byte("hello"), false, true},
161+
}
162+
163+
for _, tc := range testCases {
164+
tc := tc
165+
t.Run(tc.name, func(t *testing.T) {
166+
t.Parallel()
167+
filePath := setupTestFile(t, nil, tc.createDir)
168+
169+
err := createAndWriteBytesToFile(filePath, tc.content)
170+
if (err != nil) != tc.wantErr {
171+
t.Fatalf("createAndWriteBytesToFile() error = %v, wantErr %v", err, tc.wantErr)
172+
}
173+
174+
if !tc.wantErr {
175+
got, err := os.ReadFile(filePath)
176+
if err != nil {
177+
t.Fatalf("ReadFile() error = %v", err)
178+
}
179+
if diff := cmp.Diff(tc.content, got); diff != "" {
180+
t.Errorf("createAndWriteBytesToFile() mismatch (-want +got):\n%s", diff)
181+
}
182+
}
183+
})
184+
}
185+
}

0 commit comments

Comments
 (0)