Skip to content

Commit a3dd1ae

Browse files
committed
feat: Enhance platform-specific permission handling in 'take' command tests
- Updated 'take_test.go' to handle permission changes differently based on the operating system, specifically adding support for Windows using 'icacls'. - Modified the temporary file creation in 'take.go' to ensure the correct file extension is used, defaulting to '.tar.gz' if none is provided. These changes improve the cross-platform compatibility and robustness of the 'take' command, ensuring proper permission management and file handling across different environments. Signed-off-by: Alessandro De Blasis <[email protected]>
1 parent 4b49e9d commit a3dd1ae

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

pkg/take/take.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,12 @@ func handleTarballURL(opts Options) Result {
172172
}
173173
defer os.RemoveAll(tmpDir)
174174

175-
// Create temporary file
176-
tmpFile, err := os.CreateTemp(tmpDir, "archive-*.tar.*")
175+
// Create temporary file with proper extension
176+
ext := filepath.Ext(opts.Path)
177+
if ext == "" {
178+
ext = ".tar.gz" // default extension
179+
}
180+
tmpFile, err := os.CreateTemp(tmpDir, "archive-*"+ext)
177181
if err != nil {
178182
return Result{Error: err}
179183
}

pkg/take/take_test.go

+16-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os/exec"
1010
"archive/zip"
1111
"io"
12+
"runtime"
1213
)
1314

1415
type testCase struct {
@@ -278,12 +279,24 @@ func TestTake(t *testing.T) {
278279
if err := os.MkdirAll(path, 0755); err != nil {
279280
t.Fatalf("Failed to create noperm directory: %v", err)
280281
}
281-
if err := os.Chmod(path, 0000); err != nil {
282-
t.Fatalf("Failed to change permissions: %v", err)
282+
if runtime.GOOS == "windows" {
283+
cmd := exec.Command("icacls", path, "/deny", "Everyone:(OI)(CI)F")
284+
if err := cmd.Run(); err != nil {
285+
t.Fatalf("Failed to set permissions: %v", err)
286+
}
287+
} else {
288+
if err := os.Chmod(path, 0000); err != nil {
289+
t.Fatalf("Failed to change permissions: %v", err)
290+
}
283291
}
284292
},
285293
cleanup: func() error {
286-
return os.Chmod(tmpPath("noperm"), 0755)
294+
path := tmpPath("noperm")
295+
if runtime.GOOS == "windows" {
296+
cmd := exec.Command("icacls", path, "/reset")
297+
return cmd.Run()
298+
}
299+
return os.Chmod(path, 0755)
287300
},
288301
opts: Options{
289302
Path: tmpPath("noperm/child"),

0 commit comments

Comments
 (0)