blob: 10244e21d69454c0d84cb825d54843fe902f5c99 [file] [log] [blame]
Alan Donovan6ad27d02022-12-16 16:31:15 -05001// Copyright 2022 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package robustio_test
6
7import (
8 "os"
9 "path/filepath"
Alan Donovan3856a5d2023-01-05 16:17:44 -050010 "runtime"
Alan Donovan6ad27d02022-12-16 16:31:15 -050011 "testing"
Robert Findleyae242ec2023-01-19 16:41:08 -050012 "time"
Alan Donovan6ad27d02022-12-16 16:31:15 -050013
Tim King33071fb2022-12-30 17:02:30 -080014 "golang.org/x/tools/internal/robustio"
Alan Donovan6ad27d02022-12-16 16:31:15 -050015)
16
Robert Findleyae242ec2023-01-19 16:41:08 -050017func TestFileInfo(t *testing.T) {
Alan Donovan6ad27d02022-12-16 16:31:15 -050018 // A nonexistent file has no ID.
19 nonexistent := filepath.Join(t.TempDir(), "nonexistent")
Robert Findleyae242ec2023-01-19 16:41:08 -050020 if _, _, err := robustio.GetFileID(nonexistent); err == nil {
Alan Donovan6ad27d02022-12-16 16:31:15 -050021 t.Fatalf("GetFileID(nonexistent) succeeded unexpectedly")
22 }
23
24 // A regular file has an ID.
25 real := filepath.Join(t.TempDir(), "real")
26 if err := os.WriteFile(real, nil, 0644); err != nil {
27 t.Fatalf("can't create regular file: %v", err)
28 }
Robert Findleyae242ec2023-01-19 16:41:08 -050029 realID, realMtime, err := robustio.GetFileID(real)
Alan Donovan6ad27d02022-12-16 16:31:15 -050030 if err != nil {
31 t.Fatalf("can't get ID of regular file: %v", err)
32 }
33
Robert Findleyae242ec2023-01-19 16:41:08 -050034 // Sleep so that we get a new mtime for subsequent writes.
35 time.Sleep(2 * time.Second)
36
Alan Donovan6ad27d02022-12-16 16:31:15 -050037 // A second regular file has a different ID.
38 real2 := filepath.Join(t.TempDir(), "real2")
39 if err := os.WriteFile(real2, nil, 0644); err != nil {
40 t.Fatalf("can't create second regular file: %v", err)
41 }
Robert Findleyae242ec2023-01-19 16:41:08 -050042 real2ID, real2Mtime, err := robustio.GetFileID(real2)
Alan Donovan6ad27d02022-12-16 16:31:15 -050043 if err != nil {
44 t.Fatalf("can't get ID of second regular file: %v", err)
45 }
46 if realID == real2ID {
Robert Findleyae242ec2023-01-19 16:41:08 -050047 t.Errorf("realID %+v == real2ID %+v", realID, real2ID)
48 }
49 if realMtime.Equal(real2Mtime) {
50 t.Errorf("realMtime %v == real2Mtime %v", realMtime, real2Mtime)
Alan Donovan6ad27d02022-12-16 16:31:15 -050051 }
52
53 // A symbolic link has the same ID as its target.
Alan Donovan3856a5d2023-01-05 16:17:44 -050054 if runtime.GOOS != "plan9" {
55 symlink := filepath.Join(t.TempDir(), "symlink")
56 if err := os.Symlink(real, symlink); err != nil {
57 t.Fatalf("can't create symbolic link: %v", err)
58 }
Robert Findleyae242ec2023-01-19 16:41:08 -050059 symlinkID, symlinkMtime, err := robustio.GetFileID(symlink)
Alan Donovan3856a5d2023-01-05 16:17:44 -050060 if err != nil {
61 t.Fatalf("can't get ID of symbolic link: %v", err)
62 }
63 if realID != symlinkID {
64 t.Errorf("realID %+v != symlinkID %+v", realID, symlinkID)
65 }
Robert Findleyae242ec2023-01-19 16:41:08 -050066 if !realMtime.Equal(symlinkMtime) {
67 t.Errorf("realMtime %v != symlinkMtime %v", realMtime, symlinkMtime)
68 }
Alan Donovan5c176b12023-01-17 12:24:46 -050069 }
Alan Donovan6ad27d02022-12-16 16:31:15 -050070
Alan Donovan5c176b12023-01-17 12:24:46 -050071 // Two hard-linked files have the same ID.
72 if runtime.GOOS != "plan9" && runtime.GOOS != "android" {
Alan Donovan3856a5d2023-01-05 16:17:44 -050073 hardlink := filepath.Join(t.TempDir(), "hardlink")
74 if err := os.Link(real, hardlink); err != nil {
75 t.Fatal(err)
76 }
Robert Findleyae242ec2023-01-19 16:41:08 -050077 hardlinkID, hardlinkMtime, err := robustio.GetFileID(hardlink)
Alan Donovan3856a5d2023-01-05 16:17:44 -050078 if err != nil {
79 t.Fatalf("can't get ID of hard link: %v", err)
80 }
Robert Findleyae242ec2023-01-19 16:41:08 -050081 if realID != hardlinkID {
Alan Donovan3856a5d2023-01-05 16:17:44 -050082 t.Errorf("realID %+v != hardlinkID %+v", realID, hardlinkID)
83 }
Robert Findleyae242ec2023-01-19 16:41:08 -050084 if !realMtime.Equal(hardlinkMtime) {
85 t.Errorf("realMtime %v != hardlinkMtime %v", realMtime, hardlinkMtime)
86 }
Alan Donovan6ad27d02022-12-16 16:31:15 -050087 }
88}