Skip to content

Commit

Permalink
[#9] fix: Clean will not remove file unless they're in the working di…
Browse files Browse the repository at this point in the history
…rectory
  • Loading branch information
majohn-r committed May 18, 2024
1 parent aa1ef3c commit 56ae0c7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
25 changes: 16 additions & 9 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,25 @@ var (
// calls os.Exit(); this is to prevent callers from deceptively removing files
// they shouldn't.
func Clean(files []string) {
workingFS := os.DirFS(WorkingDir())
for _, file := range files {
if containsBackDir(file) {
fmt.Fprintf(os.Stderr, "file %q will not be removed, exiting the build\n", file)
exit(1)
}
fileSystem.Remove(filepath.Join(WorkingDir(), file))
openFile, err := workingFS.Open(file)
if err == nil {
openFile.Close()
fileSystem.Remove(filepath.Join(WorkingDir(), file))
}
}
}

func containsBackDir(path string) bool {
if !strings.Contains(path, "..") {
return false
}
if strings.Contains(path, "\\") {
path = strings.ReplaceAll(path, "\\", "/")
}
path = canonicalizePath(path)
if path == ".." {
return true
}
Expand All @@ -53,6 +56,13 @@ func containsBackDir(path string) bool {
return containsBackDir(strings.TrimSuffix(dir, "/"))
}

func canonicalizePath(path string) string {
if strings.Contains(path, "\\") {
return strings.ReplaceAll(path, "\\", "/")
}
return path
}

// Format runs the gofmt tool to repair the formatting of each source file;
// returns false if the command fails
func Format(a *goyek.A) bool {
Expand Down Expand Up @@ -251,15 +261,12 @@ func allDirs(top string) ([]string, error) {
if !topIsDir {
return nil, fmt.Errorf("%q is not a directory", top)
}
if strings.Contains(top, "\\") {
top = strings.ReplaceAll(top, "\\", "/")
}
top = canonicalizePath(top)
entries, _ := afero.ReadDir(fileSystem, top)
dirs := []string{top}
for _, entry := range entries {
if entry.IsDir() {
subDirectory := filepath.Join(top, entry.Name())
subDirs, _ := allDirs(subDirectory)
subDirs, _ := allDirs(filepath.Join(top, entry.Name()))
dirs = append(dirs, subDirs...)
}
}
Expand Down
14 changes: 6 additions & 8 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@ import (
)

func TestClean(t *testing.T) {
originalFileSystem := fileSystem
// note: cannot use memory mapped filesystem; Clean relies on using the os
// filesystem to make sure all files are within the working directory
originalWorkingDir := workingDir
originalExit := exit
workingDir = "a/b/c"
defer func() {
fileSystem = originalFileSystem
workingDir = originalWorkingDir
exit = originalExit
fileSystem.RemoveAll("a")
}()
fileSystem = afero.NewMemMapFs()
fileSystem.MkdirAll("a/b/c", 0o755)
workingDir = "a/b/c"
afero.WriteFile(fileSystem, "a/b/c/myFile", []byte("foo"), 0o644)
afero.WriteFile(fileSystem, "a/b/c/myOtherFile", []byte(""), 0o644)
tests := map[string]struct {
files []string
wantExitCalled bool
}{
"no files": {files: nil},
"no problems": {files: []string{"myFile", "myOtherFile", "myNonExistentFile"}},
"no problems": {files: []string{"myFile", "myOtherFile", "c:\\myNonExistentFile"}},
"illegal path": {files: []string{"foo/../../bar"}, wantExitCalled: true},
}
for name, tt := range tests {
Expand All @@ -46,9 +46,7 @@ func TestClean(t *testing.T) {
Clean(tt.files)
for _, file := range tt.files {
f := filepath.Join(workingDir, file)
if fileExists, err := afero.Exists(fileSystem, f); err != nil {
t.Errorf("Clean: something went wrong verifying the non-existence of %q: %v", f, err)
} else if fileExists {
if fileExists, _ := afero.Exists(fileSystem, f); fileExists {
t.Errorf("Clean failed to delete %q", f)
}
}
Expand Down

0 comments on commit 56ae0c7

Please sign in to comment.