github.com/GoogleCloudPlatform/compute-image-tools/cli_tools@v0.0.0-20240516224744-de2dabc4ed1b/common/utils/files/files_test.go (about) 1 // Copyright 2020 Google Inc. All Rights Reserved. 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 // http://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 files 16 17 import ( 18 "io/ioutil" 19 "os" 20 "path" 21 "path/filepath" 22 "testing" 23 24 "github.com/google/uuid" 25 "github.com/stretchr/testify/assert" 26 ) 27 28 func TestExists(t *testing.T) { 29 tests := []struct { 30 name string 31 path string 32 expected bool 33 }{ 34 { 35 name: "Return true when directory exists", 36 path: makeTempDir(t), 37 expected: true, 38 }, 39 { 40 name: "Return true when file exists", 41 path: makeTempFile(t), 42 expected: true, 43 }, 44 { 45 name: "Return false when file not found", 46 path: makeNotExistantFile(t), 47 }, 48 } 49 for _, tt := range tests { 50 t.Run(tt.name, func(t *testing.T) { 51 assert.Equal(t, tt.expected, Exists(tt.path)) 52 }) 53 } 54 } 55 56 func TestDirectoryExists(t *testing.T) { 57 tests := []struct { 58 name string 59 path string 60 expected bool 61 }{ 62 { 63 name: "Return true when directory exists", 64 path: makeTempDir(t), 65 expected: true, 66 }, 67 { 68 name: "Return true when path is a file", 69 path: makeTempFile(t), 70 }, 71 { 72 name: "Return false when path not found", 73 path: makeNotExistantFile(t), 74 }, 75 } 76 for _, tt := range tests { 77 t.Run(tt.name, func(t *testing.T) { 78 assert.Equal(t, tt.expected, DirectoryExists(tt.path)) 79 }) 80 } 81 } 82 83 func TestMakeAbsolute_HappyCase(t *testing.T) { 84 // Return to the test directory after running the test. 85 curr, err := os.Getwd() 86 if err != nil { 87 t.Fatal(err) 88 } 89 defer os.Chdir(curr) 90 91 // Change to a temporary directory, and write a file. 92 tmpDir := makeTempDir(t) 93 err = os.Chdir(tmpDir) 94 if err != nil { 95 t.Fatal(err) 96 } 97 _, err = os.Create("tmp.txt") 98 if err != nil { 99 t.Fatal(err) 100 } 101 102 expected := path.Join(tmpDir, "tmp.txt") 103 actual := MakeAbsolute("tmp.txt") 104 assert.Equal(t, expected, actual) 105 } 106 107 func TestMakeAbsolute_PanicWhenTargetDoesntExist(t *testing.T) { 108 assert.Panics(t, func() { 109 MakeAbsolute(makeNotExistantFile(t)) 110 }) 111 } 112 113 func TestMakeAbsolute_WhenParamIsAbsolute_DontModify(t *testing.T) { 114 absoluteDir := makeTempDir(t) 115 assert.True(t, filepath.IsAbs(absoluteDir)) 116 assert.Equal(t, absoluteDir, MakeAbsolute(absoluteDir)) 117 } 118 119 // makeNotExistantFile returns a filesystem path that is guaranteed to *not* point to a file. 120 func makeNotExistantFile(t *testing.T) string { 121 notAFile := uuid.New().String() 122 _, err := os.Stat(notAFile) 123 if !os.IsNotExist(err) { 124 t.Fatalf("Expected %s to not exist", notAFile) 125 } 126 return notAFile 127 } 128 129 // makeTempFile returns the path to a new file in a temporary directory. 130 func makeTempFile(t *testing.T) string { 131 tmpFileObj, err := ioutil.TempFile("", "*.txt") 132 assert.NoError(t, err) 133 tmpFile := tmpFileObj.Name() 134 return tmpFile 135 } 136 137 // makeTempDir returns the path to a new temporary directory. 138 func makeTempDir(t *testing.T) string { 139 tmpDir, err := ioutil.TempDir("", "") 140 assert.NoError(t, err) 141 return tmpDir 142 }