Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes runme when it attempts to access unreadable files or directories #646

Merged
Merged
48 changes: 48 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package main

import (
"bufio"
"bytes"
"os"
"os/exec"
Expand All @@ -16,6 +17,42 @@ import (
"github.com/stretchr/testify/require"
)

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 TestMain(m *testing.M) {
os.Exit(testscript.RunMain(m, map[string]func() int{
"runme": root,
Expand All @@ -33,6 +70,17 @@ func TestRunme(t *testing.T) {
})
}

func TestRunmeFilePermissions(t *testing.T) {
if isDocker() {
return
}

testscript.Run(t, testscript.Params{
Dir: "testdata/permissions",
ContinueOnError: true,
})
}

func TestRunmeFlags(t *testing.T) {
testscript.Run(t, testscript.Params{
Dir: "testdata/flags",
Expand Down
12 changes: 12 additions & 0 deletions pkg/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,13 @@ func (p *Project) loadFromDirectory(

err := util.Walk(p.fs, ".", func(path string, info fs.FileInfo, err error) error {
if err != nil {
if errors.Is(err, os.ErrPermission) {
p.logger.Warn("permission denied", zap.String("path", path), zap.Error(err))
if info.IsDir() {
return filepath.SkipDir
}
return nil
}
return err
}

Expand Down Expand Up @@ -455,6 +462,11 @@ func (p *Project) extractTasksFromFile(
})

if err != nil {
if errors.Is(err, os.ErrPermission) {
p.logger.Warn("permission denied", zap.String("path", path), zap.Error(err))
return
}

p.send(ctx, eventc, LoadEvent{
Type: LoadEventError,
Data: LoadEventErrorData{Err: err},
Expand Down
27 changes: 27 additions & 0 deletions testdata/permissions/basic.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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":"command"}
$ echo "Hello, runme!"
```

-- output.txt --
NAME FILE FIRST COMMAND DESCRIPTION NAMED
command README.md echo "Hello, runme!" Yes