Skip to content

Commit 451ac41

Browse files
committed
Add a unit test for touch()
1 parent c3a49e9 commit 451ac41

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

Diff for: cmd/git-sync/main_test.go

+57
Original file line numberDiff line numberDiff line change
@@ -717,3 +717,60 @@ func TestRemoveDirContents(t *testing.T) {
717717
t.Errorf("unexpected success for non-existent dir")
718718
}
719719
}
720+
721+
func TestTouch(t *testing.T) {
722+
root := absPath(t.TempDir())
723+
724+
// Make a dir and get info.
725+
dirPath := root.Join("dir")
726+
if err := os.MkdirAll(dirPath.String(), 0755); err != nil {
727+
t.Fatalf("can't create dir: %v", err)
728+
}
729+
730+
// Make a file and get info.
731+
filePath := root.Join("file")
732+
if file, err := os.Create(filePath.String()); err != nil {
733+
t.Fatalf("can't create file: %v", err)
734+
} else {
735+
file.Close()
736+
}
737+
738+
// Make sure a newfile does not exist.
739+
newfilePath := root.Join("newfile")
740+
if fi, err := os.Stat(newfilePath.String()); err == nil {
741+
t.Fatalf("unexpected newfile: %v", fi)
742+
} else if !os.IsNotExist(err) {
743+
t.Fatalf("can't stat newfile: %v", err)
744+
}
745+
746+
time.Sleep(500 * time.Millisecond)
747+
stamp := time.Now()
748+
time.Sleep(100 * time.Millisecond)
749+
750+
touch(dirPath)
751+
if dirInfo, err := os.Stat(dirPath.String()); err != nil {
752+
t.Fatalf("can't stat dir: %v", err)
753+
} else if !dirInfo.IsDir() {
754+
t.Errorf("touch(dir) is no longer a dir: %v", dirInfo)
755+
} else if !dirInfo.ModTime().After(stamp) {
756+
t.Errorf("touch(dir) mtime %v is not after %v", dirInfo.ModTime(), stamp)
757+
}
758+
759+
touch(filePath)
760+
if fileInfo, err := os.Stat(filePath.String()); err != nil {
761+
t.Fatalf("can't stat file: %v", err)
762+
} else if fileInfo.IsDir() {
763+
t.Errorf("touch(file) is no longer a file: %v", fileInfo)
764+
} else if !fileInfo.ModTime().After(stamp) {
765+
t.Errorf("touch(file) mtime %v is not after %v", fileInfo.ModTime(), stamp)
766+
}
767+
768+
touch(newfilePath)
769+
if newfileInfo, err := os.Stat(newfilePath.String()); err != nil {
770+
t.Fatalf("can't stat newfile: %v", err)
771+
} else if newfileInfo.IsDir() {
772+
t.Errorf("touch(newfile) is not a file: %v", newfileInfo)
773+
} else if !newfileInfo.ModTime().After(stamp) {
774+
t.Errorf("touch(newfile) mtime %v is not after %v", newfileInfo.ModTime(), stamp)
775+
}
776+
}

0 commit comments

Comments
 (0)