Fix test case by correcting cleanup order

This commit is contained in:
aneesh-n 2024-06-05 23:10:29 -06:00
parent 2101dfe448
commit 4f053da06a
No known key found for this signature in database
GPG key ID: 6F5A52831C046F44

View file

@ -138,36 +138,34 @@ func TestSetFileEa(t *testing.T) {
// The code below was refactored from github.com/Microsoft/go-winio/blob/a7564fd482feb903f9562a135f1317fd3b480739/ea_test.go // The code below was refactored from github.com/Microsoft/go-winio/blob/a7564fd482feb903f9562a135f1317fd3b480739/ea_test.go
// under MIT license. // under MIT license.
func TestSetGetFileEA(t *testing.T) { func TestSetGetFileEA(t *testing.T) {
testfilePath := setupTestFile(t) testFilePath, testFile := setupTestFile(t)
defer cleanupTestFile(t, testfilePath) testEAs := generateTestEAs(t, 3, testFilePath)
fileHandle := openFile(t, testFilePath, windows.FILE_ATTRIBUTE_NORMAL)
defer closeFileHandle(t, testFilePath, testFile, fileHandle)
testEAs := generateTestEAs(t, 3, testfilePath) testSetGetEA(t, testFilePath, fileHandle, testEAs)
fileHandle := openFile(t, testfilePath, windows.FILE_ATTRIBUTE_NORMAL)
defer closeFileHandle(t, fileHandle, testfilePath)
testSetGetEA(t, fileHandle, testEAs, testfilePath)
} }
// The code is new code and reuses code refactored from github.com/Microsoft/go-winio/blob/a7564fd482feb903f9562a135f1317fd3b480739/ea_test.go // The code is new code and reuses code refactored from github.com/Microsoft/go-winio/blob/a7564fd482feb903f9562a135f1317fd3b480739/ea_test.go
// under MIT license. // under MIT license.
func TestSetGetFolderEA(t *testing.T) { func TestSetGetFolderEA(t *testing.T) {
testfolderPath := setupTestFolder(t) testFolderPath := setupTestFolder(t)
defer cleanupTestFolder(t, testfolderPath)
testEAs := generateTestEAs(t, 3, testfolderPath) testEAs := generateTestEAs(t, 3, testFolderPath)
fileHandle := openFile(t, testfolderPath, windows.FILE_ATTRIBUTE_NORMAL|windows.FILE_FLAG_BACKUP_SEMANTICS) fileHandle := openFile(t, testFolderPath, windows.FILE_ATTRIBUTE_NORMAL|windows.FILE_FLAG_BACKUP_SEMANTICS)
defer closeFileHandle(t, fileHandle, testfolderPath) defer closeFileHandle(t, testFolderPath, nil, fileHandle)
testSetGetEA(t, fileHandle, testEAs, testfolderPath) testSetGetEA(t, testFolderPath, fileHandle, testEAs)
} }
func setupTestFile(t *testing.T) string { func setupTestFile(t *testing.T) (testFilePath string, testFile *os.File) {
tempDir := t.TempDir() tempDir := t.TempDir()
testfilePath := filepath.Join(tempDir, "testfile.txt") testFilePath = filepath.Join(tempDir, "testfile.txt")
if _, err := os.Create(testfilePath); err != nil { var err error
if testFile, err = os.Create(testFilePath); err != nil {
t.Fatalf("failed to create temporary file: %s", err) t.Fatalf("failed to create temporary file: %s", err)
} }
return testfilePath return testFilePath, testFile
} }
func setupTestFolder(t *testing.T) string { func setupTestFolder(t *testing.T) string {
@ -191,6 +189,18 @@ func generateTestEAs(t *testing.T, nAttrs int, path string) []ExtendedAttribute
return testEAs return testEAs
} }
func getRandomInt() int64 {
nBig, err := rand.Int(rand.Reader, big.NewInt(27))
if err != nil {
panic(err)
}
n := nBig.Int64()
if n == 0 {
n = getRandomInt()
}
return n
}
func openFile(t *testing.T, path string, attributes uint32) windows.Handle { func openFile(t *testing.T, path string, attributes uint32) windows.Handle {
utf16Path := windows.StringToUTF16Ptr(path) utf16Path := windows.StringToUTF16Ptr(path)
fileAccessRightReadWriteEA := uint32(0x8 | 0x10) fileAccessRightReadWriteEA := uint32(0x8 | 0x10)
@ -201,13 +211,26 @@ func openFile(t *testing.T, path string, attributes uint32) windows.Handle {
return fileHandle return fileHandle
} }
func closeFileHandle(t *testing.T, handle windows.Handle, path string) { func closeFileHandle(t *testing.T, testfilePath string, testFile *os.File, handle windows.Handle) {
if testFile != nil {
err := testFile.Close()
if err != nil {
t.Logf("Error closing file %s: %v\n", testFile.Name(), err)
}
}
if err := windows.Close(handle); err != nil { if err := windows.Close(handle); err != nil {
t.Logf("Error closing file handle %s: %v\n", path, err) t.Logf("Error closing file handle %s: %v\n", testfilePath, err)
}
cleanupTestFile(t, testfilePath)
}
func cleanupTestFile(t *testing.T, path string) {
if err := os.Remove(path); err != nil {
t.Logf("Error removing file/folder %s: %v\n", path, err)
} }
} }
func testSetGetEA(t *testing.T, handle windows.Handle, testEAs []ExtendedAttribute, path string) { func testSetGetEA(t *testing.T, path string, handle windows.Handle, testEAs []ExtendedAttribute) {
if err := SetFileEA(handle, testEAs); err != nil { if err := SetFileEA(handle, testEAs); err != nil {
t.Fatalf("set EA for path %s failed: %s", path, err) t.Fatalf("set EA for path %s failed: %s", path, err)
} }
@ -222,27 +245,3 @@ func testSetGetEA(t *testing.T, handle windows.Handle, testEAs []ExtendedAttribu
t.Fatalf("EAs read from path %s don't match", path) t.Fatalf("EAs read from path %s don't match", path)
} }
} }
func cleanupTestFile(t *testing.T, path string) {
if err := os.Remove(path); err != nil {
t.Logf("Error removing file %s: %v\n", path, err)
}
}
func cleanupTestFolder(t *testing.T, path string) {
if err := os.Remove(path); err != nil {
t.Logf("Error removing folder %s: %v\n", path, err)
}
}
func getRandomInt() int64 {
nBig, err := rand.Int(rand.Reader, big.NewInt(27))
if err != nil {
panic(err)
}
n := nBig.Int64()
if n == 0 {
n = getRandomInt()
}
return n
}