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/>.
26 ft "ruderich.org/simon/safcm/cmd/safcm-remote/sync/filetest"
27 "ruderich.org/simon/safcm/testutil"
30 func TestHandle(t *testing.T) {
31 cwd, err := os.Getwd()
37 err = os.RemoveAll("testdata")
41 err = os.Mkdir("testdata", 0700)
46 // Set umask to test mode for new files
47 umask := syscall.Umask(027)
48 defer syscall.Umask(umask)
52 Mode: fs.ModeDir | 0700,
54 _, uid, _, gid := ft.CurrentUserAndGroup()
67 // TODO: Add tests for chown and run them only as root
79 fmt.Errorf("\"file\": file does not exist, use -create"),
93 Data: []byte("line\n"),
97 fmt.Sprintf(`"file": created file (%d/%d -rw-r-----)`, uid, gid),
98 `"file": added line "line"`,
103 "missing: create (empty line)",
112 fmt.Errorf("empty line"),
115 "missing: create (newline)",
124 fmt.Errorf("line must not contain newlines: \"line\\n\""),
133 ft.CreateFile("file", "line\n", 0641)
140 Data: []byte("line\n"),
153 ft.CreateFile("file", "existing\n", 0641)
160 Data: []byte("existing\nline\n"),
164 `"file": added line "line"`,
169 "exists: append (no newline in file)",
174 ft.CreateFile("file", "existing", 0641)
181 Data: []byte("existing\nline\n"),
185 `"file": added missing trailing newline`,
186 `"file": added line "line"`,
191 "exists: append (only trailing newline I)",
196 ft.CreateFile("file", "first\nline", 0641)
203 Data: []byte("first\nline\n"),
207 `"file": added missing trailing newline`,
212 "exists: append (only trailing newline II)",
217 ft.CreateFile("file", "first\nline", 0641)
224 Data: []byte("first\nline\n"),
228 `"file": added missing trailing newline`,
233 "exists: append (partial line)",
238 ft.CreateFile("file", "# line with spaces\n", 0641)
245 Data: []byte("# line with spaces\nline with spaces\n"),
249 `"file": added line "line with spaces"`,
260 ft.CreateSymlink("file", "target")
266 Mode: fs.ModeSymlink | 0777,
267 Data: []byte("target"),
271 fmt.Errorf("open file: too many levels of symbolic links"),
279 ft.CreateFifo("file", 0640)
285 Mode: fs.ModeNamedPipe | 0640,
289 fmt.Errorf("\"file\" is not a regular file but p---------"),
293 for _, tc := range tests {
294 t.Run(tc.name, func(t *testing.T) {
295 // Create separate test directory for each test case
296 path := filepath.Join(cwd, "testdata", tc.name)
297 err = os.Mkdir(path, 0700)
306 if tc.prepare != nil {
310 changes, err := handle(tc.path, tc.line, tc.create)
311 testutil.AssertEqual(t, "changes",
312 changes, tc.expChanges)
313 testutil.AssertErrorEqual(t, "err", err, tc.expErr)
315 files, err := ft.WalkDir(path)
319 testutil.AssertEqual(t, "files", files, tc.expFiles)
324 err = os.RemoveAll(filepath.Join(cwd, "testdata"))