github.com/verrazzano/verrazzano@v1.7.0/pkg/os/file_test.go (about) 1 // Copyright (c) 2021, 2022, Oracle and/or its affiliates. 2 // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. 3 package os 4 5 import ( 6 "fmt" 7 "os" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 "go.uber.org/zap" 12 ) 13 14 // TestCreateTempFile tests the CreateTempFile function 15 // GIVEN a call to CreateTempFile 16 // 17 // WHEN with a valid prefix and/or suffix 18 // THEN the temp file is created 19 func TestCreateTempFile(t *testing.T) { 20 var tests = []struct { 21 name string 22 createPattern string 23 }{ 24 {name: "WithPrefixAndSuffix", createPattern: "test1-*.yaml"}, 25 {name: "WithPrefixOnly", createPattern: "test2-*"}, 26 {name: "WithSuffixOnly", createPattern: "*-test3"}, 27 {name: "NoPrefixNorSuffix", createPattern: "test4"}, 28 } 29 30 for _, test := range tests { 31 t.Run(test.name, func(t *testing.T) { 32 assert := assert.New(t) 33 temp, err := CreateTempFile(test.createPattern, nil) 34 defer RemoveTempFiles(zap.S(), temp.Name()) 35 assert.FileExists(temp.Name()) 36 fmt.Println(temp.Name()) 37 assert.NoErrorf(err, "Unable to create temp file %s: %s", temp.Name(), err) 38 }) 39 } 40 41 } 42 43 // TestRemoveTempFiles tests the RemoveTempFiles function 44 // GIVEN a call to RemoveTempFiles 45 // 46 // WHEN with a valid prefix and/or suffix 47 // THEN any matching temp files are correctly deleted 48 func TestRemoveTempFiles(t *testing.T) { 49 50 tests := []struct { 51 name string 52 createPattern string 53 deletePattern string 54 }{ 55 {name: "WithPrefixAndSuffix", createPattern: "test1-*.yaml", deletePattern: "test1-.*\\.yaml"}, 56 {name: "WithPrefixOnly", createPattern: "test2-*", deletePattern: "test2-.*"}, 57 {name: "WithSuffixOnly", createPattern: "*-mike", deletePattern: ".*-mike"}, 58 } 59 for _, test := range tests { 60 t.Run(test.name, func(t *testing.T) { 61 assert := assert.New(t) 62 temp, err := os.CreateTemp(os.TempDir(), test.createPattern) 63 assert.NoErrorf(err, "Unable to create temp file %s for testing: %s", temp.Name(), err) 64 assert.FileExists(temp.Name()) 65 err = RemoveTempFiles(zap.S(), test.deletePattern) 66 assert.NoErrorf(err, "Unable to remove temp file %s: %s", temp.Name(), err) 67 assert.NoFileExists(temp.Name()) 68 }) 69 } 70 // Verify error returned on invalid regex pattern 71 assert.Error(t, RemoveTempFiles(zap.S(), "[")) 72 } 73 74 // TestFileExists tests the FileExists function 75 // GIVEN a call to FileExists 76 // 77 // WHEN with a valid file path 78 // THEN the file name is checked for existence 79 func TestFileExists(t *testing.T) { 80 tests := []struct { 81 name string 82 createPattern string 83 }{ 84 {name: "WithPrefixAndSuffix", createPattern: "test1-*.yaml"}, 85 {name: "WithPrefixOnly", createPattern: "test2-*"}, 86 {name: "WithSuffixOnly", createPattern: "*-test3"}, 87 {name: "NoPrefixNorSuffix", createPattern: "test"}, 88 } 89 for _, test := range tests { 90 t.Run(test.name, func(t *testing.T) { 91 assert := assert.New(t) 92 tmpFile, err := CreateTempFile(test.createPattern, nil) 93 assert.NoErrorf(err, "Unable to create temp file %s for testing: %s", tmpFile, err) 94 exists, err := FileExists(tmpFile.Name()) 95 assert.True(exists) 96 assert.NoError(err) 97 //Returns false when file does not exist 98 exists, err = FileExists("nofile") 99 assert.False(exists) 100 assert.NoError(err) 101 }) 102 } 103 }