diff options
| author | Shulhan <ms@kilabit.info> | 2026-02-15 13:44:39 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2026-02-15 13:45:38 +0700 |
| commit | 83a162bfa6e6eba78ae439e193682c19b9cd4744 (patch) | |
| tree | 8588115d422a34aeaefa22e759ef1f19d992784f /awwan_sudo_test.go | |
| parent | 19d58e9a4c900aec1d63de45a655657c760b1235 (diff) | |
| download | awwan-83a162bfa6e6eba78ae439e193682c19b9cd4744.tar.xz | |
all: fix chmod/chown if destination is directory
Given the following command
#put!+0644 src/file dst/
If the dst is a directory, it would cause the directory permission
changes to 0644.
This changes fix it by checking if the destination is a directory first.
If we cannot stat the dst, skip the chmod/chown command.
Diffstat (limited to 'awwan_sudo_test.go')
| -rw-r--r-- | awwan_sudo_test.go | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/awwan_sudo_test.go b/awwan_sudo_test.go index eb26202..8becc0b 100644 --- a/awwan_sudo_test.go +++ b/awwan_sudo_test.go @@ -42,6 +42,11 @@ func TestAwwan_Local_SudoGet(t *testing.T) { t.Fatal(err) } + err = os.Chmod(filepath.Join(baseDir, `tmp`), 0755) + if err != nil { + t.Fatal(err) + } + var ( mockTerm = mock.ReadWriter{} @@ -141,6 +146,7 @@ func TestAwwan_Local_SudoPut(t *testing.T) { expError string expContent string expMode fs.FileMode + expDirMode fs.FileMode } // Load the test data output. @@ -157,6 +163,11 @@ func TestAwwan_Local_SudoPut(t *testing.T) { t.Fatal(err) } + err = os.Chmod(filepath.Join(baseDir, `tmp`), 0755) + if err != nil { + t.Fatal(err) + } + var cases = []testCase{{ desc: `WithTextFile`, lineRange: `7-8`, @@ -164,6 +175,7 @@ func TestAwwan_Local_SudoPut(t *testing.T) { fileDest: `/etc/plain.txt`, expContent: string(tdata.Output[`tmp/plain.txt`]), expMode: 420, + expDirMode: 0755, }, { desc: `WithMode`, lineRange: `14`, @@ -171,6 +183,7 @@ func TestAwwan_Local_SudoPut(t *testing.T) { fileDest: `/etc/sudoput_with_mode.txt`, expContent: string(tdata.Output[`tmp/plain.txt`]), expMode: 0516, + expDirMode: 0755, }, { desc: `WithOwner`, lineRange: `16`, @@ -178,6 +191,15 @@ func TestAwwan_Local_SudoPut(t *testing.T) { fileDest: `/etc/sudoput_with_owner.txt`, expContent: string(tdata.Output[`tmp/plain.txt`]), expMode: 420, + expDirMode: 0755, + }, { + desc: `WithDestinationIsDirectory`, + lineRange: `20`, + sudoPass: "awwan\nawwan\n", + fileDest: filepath.Join(baseDir, `tmp`, `plain.txt`), + expContent: string(tdata.Output[`tmp/plain.txt`]), + expMode: 0644, + expDirMode: 0755, }} var ( @@ -238,7 +260,12 @@ func TestAwwan_Local_SudoPut(t *testing.T) { if err != nil { t.Fatal(err) } - test.Assert(t, `permission`, c.expMode, fi.Mode().Perm()) + + fi, err = os.Stat(filepath.Dir(c.fileDest)) + if err != nil { + t.Fatal(err) + } + test.Assert(t, `dirMode`, c.expDirMode, fi.Mode().Perm()) } } |
