Skip to content

Commit

Permalink
Merge pull request #831 from rsteube/sandbox-quickfix
Browse files Browse the repository at this point in the history
sandbox: quickfix for work directory
  • Loading branch information
rsteube authored Jul 21, 2023
2 parents b2fac06 + d152f91 commit 5d03e18
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
29 changes: 29 additions & 0 deletions internal/common/mock.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package common

import (
"fmt"
"os"
)

type Mock struct {
Dir string
Replies map[string]string
Expand All @@ -12,3 +17,27 @@ func (m Mock) CacheDir() string {
func (m Mock) WorkDir() string {
return m.Dir + "/work/"
}

type t interface {
Name() string
Fatal(...interface{})
}

func NewMock(t t) *Mock {
tempDir, err := os.MkdirTemp(os.TempDir(), fmt.Sprintf("carapace-sandbox_%v_", t.Name()))
if err != nil {
t.Fatal("failed to create sandbox dir: " + err.Error())
}

m := &Mock{
Dir: tempDir,
Replies: make(map[string]string),
}
if err := os.Mkdir(m.CacheDir(), os.ModePerm); err != nil {
t.Fatal("failed to create sandbox cache dir: " + err.Error())
}
if err := os.Mkdir(m.WorkDir(), os.ModePerm); err != nil {
t.Fatal("failed to create sandbox work dir: " + err.Error())
}
return m
}
13 changes: 3 additions & 10 deletions pkg/sandbox/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,11 @@ type Sandbox struct {
}

func newSandbox(t *testing.T, f func() *cobra.Command) Sandbox {
tempDir, err := os.MkdirTemp(os.TempDir(), fmt.Sprintf("carapace-sandbox_%v_", t.Name()))
if err != nil {
t.Fatal("failed to create sandbox dir: " + err.Error())
}
return Sandbox{
t: t,
cmdF: f,
env: make(map[string]string),
mock: &common.Mock{
Dir: tempDir,
Replies: make(map[string]string),
},
mock: common.NewMock(t),
}
}

Expand Down Expand Up @@ -65,7 +58,7 @@ func (s *Sandbox) ClearCache() {
// Files creates files within the sandbox directory.
//
// s.Files(
// "file1.txt", "content of files1.txt",
// "file1.txt", "content of file1.txt",
// "dir1/file2.md", "content of file2.md",
// )
func (s *Sandbox) Files(args ...string) {
Expand All @@ -82,7 +75,7 @@ func (s *Sandbox) Files(args ...string) {
file := args[i]
content := args[i+1]

if strings.HasPrefix(file, "../") {
if strings.Contains(file, "..") || strings.HasPrefix(file, "/") {
s.t.Fatalf("invalid filename: %v", file)
}

Expand Down

0 comments on commit 5d03e18

Please sign in to comment.