1 // Copyright (C) 2021 Simon Ruderich
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
27 "github.com/google/go-cmp/cmp"
29 ft "ruderich.org/simon/safcm/cmd/safcm-remote/sync/filetest"
32 func TestHandle(t *testing.T) {
33 cwd, err := os.Getwd()
39 err = os.RemoveAll("testdata")
43 err = os.Mkdir("testdata", 0700)
48 // Set umask to test mode for new files
49 umask := syscall.Umask(027)
50 defer syscall.Umask(umask)
54 Mode: fs.ModeDir | 0700,
56 _, uid, _, gid := ft.CurrentUserAndGroup()
69 // TODO: Add tests for chown and run them only as root
81 fmt.Errorf("\"file\": file does not exist, use -create"),
95 Data: []byte("line\n"),
99 fmt.Sprintf(`"file": created file (%d/%d -rw-r-----)`, uid, gid),
100 `"file": added line "line"`,
105 "missing: create (empty line)",
114 fmt.Errorf("empty line"),
117 "missing: create (newline)",
126 fmt.Errorf("line must not contain newlines: \"line\\n\""),
135 ft.CreateFile("file", "line\n", 0641)
142 Data: []byte("line\n"),
155 ft.CreateFile("file", "existing\n", 0641)
162 Data: []byte("existing\nline\n"),
166 `"file": added line "line"`,
171 "exists: append (no newline in file)",
176 ft.CreateFile("file", "existing", 0641)
183 Data: []byte("existing\nline\n"),
187 `"file": added missing trailing newline`,
188 `"file": added line "line"`,
193 "exists: append (only trailing newline I)",
198 ft.CreateFile("file", "first\nline", 0641)
205 Data: []byte("first\nline\n"),
209 `"file": added missing trailing newline`,
214 "exists: append (only trailing newline II)",
219 ft.CreateFile("file", "first\nline", 0641)
226 Data: []byte("first\nline\n"),
230 `"file": added missing trailing newline`,
235 "exists: append (partial line)",
240 ft.CreateFile("file", "# line with spaces\n", 0641)
247 Data: []byte("# line with spaces\nline with spaces\n"),
251 `"file": added line "line with spaces"`,
262 ft.CreateSymlink("file", "target")
268 Mode: fs.ModeSymlink | 0777,
269 Data: []byte("target"),
273 fmt.Errorf("open file: too many levels of symbolic links"),
281 ft.CreateFifo("file", 0640)
287 Mode: fs.ModeNamedPipe | 0640,
291 fmt.Errorf("\"file\" is not a regular file but p---------"),
295 for _, tc := range tests {
296 t.Run(tc.name, func(t *testing.T) {
297 // Create separate test directory for each test case
298 path := filepath.Join(cwd, "testdata", tc.name)
299 err = os.Mkdir(path, 0700)
308 if tc.prepare != nil {
312 changes, err := handle(tc.path, tc.line, tc.create)
313 if !reflect.DeepEqual(tc.expChanges, changes) {
314 t.Errorf("changes: %s",
315 cmp.Diff(tc.expChanges, changes))
317 // Ugly but the simplest way to compare errors (including nil)
318 if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) {
319 t.Errorf("err = %#v, want %#v",
323 files, err := ft.WalkDir(path)
327 if !reflect.DeepEqual(tc.expFiles, files) {
328 t.Errorf("files: %s",
329 cmp.Diff(tc.expFiles, files))
335 err = os.RemoveAll(filepath.Join(cwd, "testdata"))