-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes runme when it attempts to access unreadable files or directories (
#646) Handling the os.ErrPermission when an error occurs while trying to read files without proper permissions. Closes: #645 --------- Co-authored-by: Sebastian Tiedtke <sebastiantiedtke@gmail.com>
- Loading branch information
1 parent
ec4e6ef
commit 7d3243d
Showing
3 changed files
with
110 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
//go:build !windows | ||
|
||
package main | ||
|
||
import ( | ||
"bufio" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/rogpeppe/go-internal/testscript" | ||
) | ||
|
||
func isDocker() bool { | ||
if _, err := os.Stat("/.dockerenv"); err == nil { | ||
return true | ||
} | ||
|
||
paths := []string{"/proc/1/cgroup", "/proc/self/cgroup"} | ||
for _, path := range paths { | ||
file, err := os.Open(path) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
scanner := bufio.NewScanner(file) | ||
isDocker := false | ||
for scanner.Scan() { | ||
if strings.Contains(scanner.Text(), "docker") || strings.Contains(scanner.Text(), "kubepods") { | ||
isDocker = true | ||
break | ||
} | ||
} | ||
|
||
if err := scanner.Err(); err != nil { | ||
_ = file.Close() | ||
return false | ||
} | ||
|
||
_ = file.Close() | ||
|
||
if isDocker { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
func TestRunmeFilePermissions(t *testing.T) { | ||
if isDocker() { | ||
t.Skip("Test skipped when running inside a Docker container") | ||
return | ||
} | ||
|
||
testscript.Run(t, testscript.Params{ | ||
Dir: "testdata/permissions", | ||
ContinueOnError: true, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
exec chmod 000 secret | ||
exec chmod 000 CONTRIBUTING.md | ||
exec runme ls | ||
cmp stdout output.txt | ||
! stderr . | ||
|
||
-- secret/README.md -- | ||
|
||
```sh {"name":"secret-command"} | ||
$ echo "This is a secret command" | ||
``` | ||
|
||
-- CONTRIBUTING.md -- | ||
|
||
```sh {"name":"contributing-cell"} | ||
$ echo "This is a secret command" | ||
``` | ||
|
||
-- README.md -- | ||
|
||
```sh {"name":"command1"} | ||
$ echo "Hello, runme 1!" | ||
``` | ||
|
||
```sh {"name":"command2"} | ||
$ echo "Hello, runme 2!" | ||
``` | ||
|
||
-- output.txt -- | ||
NAME FILE FIRST COMMAND DESCRIPTION NAMED | ||
command1 README.md echo "Hello, runme 1!" Yes | ||
command2 README.md echo "Hello, runme 2!" Yes |