pkg/archive: fixe some unclosed file-handles in tests
Also fixing a "defer in loop" warning, instead changing to use sub-tests, and simplifying some code, using os.WriteFile() instead. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
1090aaaedd
commit
34668a5945
2 changed files with 45 additions and 30 deletions
|
@ -291,8 +291,15 @@ func TestUntarPathWithInvalidDest(t *testing.T) {
|
||||||
// Create a src file
|
// Create a src file
|
||||||
srcFile := filepath.Join(tempFolder, "src")
|
srcFile := filepath.Join(tempFolder, "src")
|
||||||
tarFile := filepath.Join(tempFolder, "src.tar")
|
tarFile := filepath.Join(tempFolder, "src.tar")
|
||||||
os.Create(srcFile)
|
f, err := os.Create(srcFile)
|
||||||
os.Create(invalidDestFolder) // being a file (not dir) should cause an error
|
if assert.Check(t, err) {
|
||||||
|
_ = f.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
d, err := os.Create(invalidDestFolder) // being a file (not dir) should cause an error
|
||||||
|
if assert.Check(t, err) {
|
||||||
|
_ = d.Close()
|
||||||
|
}
|
||||||
|
|
||||||
// Translate back to Unix semantics as next exec.Command is run under sh
|
// Translate back to Unix semantics as next exec.Command is run under sh
|
||||||
srcFileU := srcFile
|
srcFileU := srcFile
|
||||||
|
@ -331,7 +338,10 @@ func TestUntarPath(t *testing.T) {
|
||||||
defer os.RemoveAll(tmpFolder)
|
defer os.RemoveAll(tmpFolder)
|
||||||
srcFile := filepath.Join(tmpFolder, "src")
|
srcFile := filepath.Join(tmpFolder, "src")
|
||||||
tarFile := filepath.Join(tmpFolder, "src.tar")
|
tarFile := filepath.Join(tmpFolder, "src.tar")
|
||||||
os.Create(filepath.Join(tmpFolder, "src"))
|
f, err := os.Create(filepath.Join(tmpFolder, "src"))
|
||||||
|
if assert.Check(t, err) {
|
||||||
|
_ = f.Close()
|
||||||
|
}
|
||||||
|
|
||||||
destFolder := filepath.Join(tmpFolder, "dest")
|
destFolder := filepath.Join(tmpFolder, "dest")
|
||||||
err = os.MkdirAll(destFolder, 0o740)
|
err = os.MkdirAll(destFolder, 0o740)
|
||||||
|
@ -370,7 +380,10 @@ func TestUntarPathWithDestinationFile(t *testing.T) {
|
||||||
defer os.RemoveAll(tmpFolder)
|
defer os.RemoveAll(tmpFolder)
|
||||||
srcFile := filepath.Join(tmpFolder, "src")
|
srcFile := filepath.Join(tmpFolder, "src")
|
||||||
tarFile := filepath.Join(tmpFolder, "src.tar")
|
tarFile := filepath.Join(tmpFolder, "src.tar")
|
||||||
os.Create(filepath.Join(tmpFolder, "src"))
|
f, err := os.Create(filepath.Join(tmpFolder, "src"))
|
||||||
|
if assert.Check(t, err) {
|
||||||
|
_ = f.Close()
|
||||||
|
}
|
||||||
|
|
||||||
// Translate back to Unix semantics as next exec.Command is run under sh
|
// Translate back to Unix semantics as next exec.Command is run under sh
|
||||||
srcFileU := srcFile
|
srcFileU := srcFile
|
||||||
|
@ -385,9 +398,9 @@ func TestUntarPathWithDestinationFile(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
destFile := filepath.Join(tmpFolder, "dest")
|
destFile := filepath.Join(tmpFolder, "dest")
|
||||||
_, err = os.Create(destFile)
|
f, err = os.Create(destFile)
|
||||||
if err != nil {
|
if assert.Check(t, err) {
|
||||||
t.Fatalf("Fail to create the destination file")
|
_ = f.Close()
|
||||||
}
|
}
|
||||||
err = defaultUntarPath(tarFile, destFile)
|
err = defaultUntarPath(tarFile, destFile)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
@ -406,7 +419,10 @@ func TestUntarPathWithDestinationSrcFileAsFolder(t *testing.T) {
|
||||||
defer os.RemoveAll(tmpFolder)
|
defer os.RemoveAll(tmpFolder)
|
||||||
srcFile := filepath.Join(tmpFolder, "src")
|
srcFile := filepath.Join(tmpFolder, "src")
|
||||||
tarFile := filepath.Join(tmpFolder, "src.tar")
|
tarFile := filepath.Join(tmpFolder, "src.tar")
|
||||||
os.Create(srcFile)
|
f, err := os.Create(srcFile)
|
||||||
|
if assert.Check(t, err) {
|
||||||
|
_ = f.Close()
|
||||||
|
}
|
||||||
|
|
||||||
// Translate back to Unix semantics as next exec.Command is run under sh
|
// Translate back to Unix semantics as next exec.Command is run under sh
|
||||||
srcFileU := srcFile
|
srcFileU := srcFile
|
||||||
|
@ -562,9 +578,9 @@ func TestCopyFileWithTarInexistentDestWillCreateIt(t *testing.T) {
|
||||||
defer os.RemoveAll(tempFolder)
|
defer os.RemoveAll(tempFolder)
|
||||||
srcFile := filepath.Join(tempFolder, "src")
|
srcFile := filepath.Join(tempFolder, "src")
|
||||||
inexistentDestFolder := filepath.Join(tempFolder, "doesnotexists")
|
inexistentDestFolder := filepath.Join(tempFolder, "doesnotexists")
|
||||||
_, err = os.Create(srcFile)
|
f, err := os.Create(srcFile)
|
||||||
if err != nil {
|
if assert.Check(t, err) {
|
||||||
t.Fatal(err)
|
_ = f.Close()
|
||||||
}
|
}
|
||||||
err = defaultCopyFileWithTar(srcFile, inexistentDestFolder)
|
err = defaultCopyFileWithTar(srcFile, inexistentDestFolder)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -800,21 +816,24 @@ func TestTarWithOptionsChownOptsAlwaysOverridesIdPair(t *testing.T) {
|
||||||
{&TarOptions{ChownOpts: &idtools.Identity{UID: 1, GID: 1}, NoLchown: true}, 1, 1},
|
{&TarOptions{ChownOpts: &idtools.Identity{UID: 1, GID: 1}, NoLchown: true}, 1, 1},
|
||||||
{&TarOptions{ChownOpts: &idtools.Identity{UID: 1000, GID: 1000}, NoLchown: true}, 1000, 1000},
|
{&TarOptions{ChownOpts: &idtools.Identity{UID: 1000, GID: 1000}, NoLchown: true}, 1000, 1000},
|
||||||
}
|
}
|
||||||
for _, testCase := range cases {
|
for _, tc := range cases {
|
||||||
reader, err := TarWithOptions(filePath, testCase.opts)
|
tc := tc
|
||||||
assert.NilError(t, err)
|
t.Run("", func(t *testing.T) {
|
||||||
tr := tar.NewReader(reader)
|
reader, err := TarWithOptions(filePath, tc.opts)
|
||||||
defer reader.Close()
|
|
||||||
for {
|
|
||||||
hdr, err := tr.Next()
|
|
||||||
if err == io.EOF {
|
|
||||||
// end of tar archive
|
|
||||||
break
|
|
||||||
}
|
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
assert.Check(t, is.Equal(hdr.Uid, testCase.expectedUID), "Uid equals expected value")
|
tr := tar.NewReader(reader)
|
||||||
assert.Check(t, is.Equal(hdr.Gid, testCase.expectedGID), "Gid equals expected value")
|
defer reader.Close()
|
||||||
}
|
for {
|
||||||
|
hdr, err := tr.Next()
|
||||||
|
if err == io.EOF {
|
||||||
|
// end of tar archive
|
||||||
|
break
|
||||||
|
}
|
||||||
|
assert.NilError(t, err)
|
||||||
|
assert.Check(t, is.Equal(hdr.Uid, tc.expectedUID), "Uid equals expected value")
|
||||||
|
assert.Check(t, is.Equal(hdr.Gid, tc.expectedGID), "Gid equals expected value")
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,14 +24,10 @@ func TestHardLinkOrder(t *testing.T) {
|
||||||
defer os.RemoveAll(src)
|
defer os.RemoveAll(src)
|
||||||
for _, name := range names {
|
for _, name := range names {
|
||||||
func() {
|
func() {
|
||||||
fh, err := os.Create(path.Join(src, name))
|
err := os.WriteFile(path.Join(src, name), msg, 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer fh.Close()
|
|
||||||
if _, err = fh.Write(msg); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
// Create dest, with changes that includes hardlinks
|
// Create dest, with changes that includes hardlinks
|
||||||
|
|
Loading…
Reference in a new issue