Skip to content

Commit

Permalink
Multiple targets matched now create a directory, added tests, fixed t…
Browse files Browse the repository at this point in the history
…ypo.
  • Loading branch information
OliverLok committed Dec 2, 2024
1 parent 8984390 commit 505c585
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 83 deletions.
45 changes: 27 additions & 18 deletions internal/common/filespec.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,39 @@ func (this *FileSpec) CopyTarget(baseDir string, destDir string, onlyContents bo
this.Path = filepath.Join(baseDir, this.Path)
}

destPath := ""
if onlyContents {
if this.Dest == "" {
destPath = destDir
} else {
destPath = filepath.Join(destDir, this.Dest)
}

} else {
filename := this.Dest
if filename == "" {
filename = filepath.Base(this.Path)
}

destPath = filepath.Join(destDir, filename)
}

paths, err := this.matchTargets()
if err != nil {
return fmt.Errorf("Failed to match target(s) for path '%s': '%w'.", this.Path, err)
}

// Create a new directory if multiple targets are matched.
if !util.PathExists(destPath) && len(paths) > 1 {
err := util.MkDir(destPath)
if err != nil {
return fmt.Errorf("Failed to make a directory: '%v'.", err)
}
}

// Loop over each matched path and copy it to the destination.
for _, path := range paths {
err := copyPath(path, this.Dest, destDir, onlyContents)
err := copyPath(path, destPath, onlyContents)
if err != nil {
return fmt.Errorf("Failed to copy target '%s': '%w'.", path, err)
}
Expand Down Expand Up @@ -269,23 +294,7 @@ func (this *FileSpec) matchTargets() ([]string, error) {
return targets, nil
}

func copyPath(fileSpecPath string, fileSpecDest string, destDir string, onlyContents bool) error {
destPath := ""
if onlyContents {
if fileSpecDest == "" {
destPath = destDir
} else {
destPath = filepath.Join(destDir, fileSpecDest)
}
} else {
filename := fileSpecDest
if filename == "" {
filename = filepath.Base(fileSpecPath)
}

destPath = filepath.Join(destDir, filename)
}

func copyPath(fileSpecPath string, destPath string, onlyContents bool) error {
var err error
if onlyContents {
err = util.CopyDirContents(fileSpecPath, destPath)
Expand Down
123 changes: 59 additions & 64 deletions internal/common/filespec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func TestFileSpecCopy(test *testing.T) {

err = testCase.Spec.CopyTarget(config.GetTestdataDir(), destDir, testCase.OnlyContents)
if err != nil {
test.Errorf("Case %d: Failed to copy target (%+v): '%v'.", i, testCase.Spec, err)
test.Errorf("Case %d: Failed to copy matching targets (%+v): '%v'.", i, testCase.Spec, err)
continue
}

Expand All @@ -108,6 +108,7 @@ func TestFileSpecCopy(test *testing.T) {
parts := strings.SplitN(dirent, PATH_SEPARATOR, 2)
if len(parts) != 2 {
test.Errorf("Case %d: Failed to split the dirent '%v'.", i, parts)
continue
}
copiedDirents = append(copiedDirents, parts[1])
}
Expand All @@ -123,109 +124,103 @@ func TestFileSpecCopy(test *testing.T) {
func getCopyTestCases() []*testCaseCopy {
return []*testCaseCopy{
&testCaseCopy{
FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "filespec_test", "spec.txt")},
false,
[]string{"spec.txt"},
Spec: FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "filespec_test", "spec.txt")},
ExpectedCopiedDirents: []string{"spec.txt"},
},
&testCaseCopy{
FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "filespec_test")},
false,
[]string{"filespec_test", "filespec_test/spec.txt", "filespec_test/spec2.txt"},
Spec: FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "filespec_test")},
ExpectedCopiedDirents: []string{"filespec_test", "filespec_test/spec.txt", "filespec_test/spec2.txt"},
},
&testCaseCopy{
FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "filespec_test")},
true,
[]string{"spec.txt", "spec2.txt"},
Spec: FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "filespec_test")},
OnlyContents: true,
ExpectedCopiedDirents: []string{"spec.txt", "spec2.txt"},
},
&testCaseCopy{
FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "filespec_test", "spec.txt"), Dest: "test.txt"},
false,
[]string{"test.txt"},
Spec: FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "filespec_test", "spe?.txt"), Dest: "test.txt"},
ExpectedCopiedDirents: []string{"test.txt"},
},
&testCaseCopy{
FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "filespec_test"), Dest: "test"},
false,
[]string{"test", "test/spec.txt", "test/spec2.txt"},
Spec: FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "filespec_test"), Dest: "test"},
ExpectedCopiedDirents: []string{"test", "test/spec.txt", "test/spec2.txt"},
},
&testCaseCopy{
FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "filespec_test"), Dest: "test"},
true,
[]string{"test", "test/spec.txt", "test/spec2.txt"},
Spec: FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "filespec_test"), Dest: "test"},
OnlyContents: true,
ExpectedCopiedDirents: []string{"test", "test/spec.txt", "test/spec2.txt"},
},
&testCaseCopy{
FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "*_test"), Dest: "test"},
true,
[]string{"test", "test/*globSpec.txt", "test/spec.txt", "test/spec2.txt"},
Spec: FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "*_test"), Dest: "test"},
OnlyContents: true,
ExpectedCopiedDirents: []string{"test", "test/*globSpec.txt", "test/spec.txt", "test/spec2.txt"},
},
&testCaseCopy{
FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "f*_test"), Dest: "test"},
true,
[]string{"test", "test/spec.txt", "test/spec2.txt"},
Spec: FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "*_test")},
OnlyContents: true,
ExpectedCopiedDirents: []string{"*globSpec.txt", "spec.txt", "spec2.txt"},
},
&testCaseCopy{
FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", `\**_test`), Dest: "test"},
true,
[]string{"test", "test/*globSpec.txt"},
Spec: FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "f*_test"), Dest: "test"},
OnlyContents: true,
ExpectedCopiedDirents: []string{"test", "test/spec.txt", "test/spec2.txt"},
},
&testCaseCopy{
FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "filespec_test", "*")},
false,
[]string{"spec.txt", "spec2.txt"},
Spec: FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", `\**_test`), Dest: "test"},
OnlyContents: true,
ExpectedCopiedDirents: []string{"test", "test/*globSpec.txt"},
},
&testCaseCopy{
FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "f*_test", "*.txt"), Dest: "test.txt"},
false,
[]string{"test.txt"},
Spec: FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "filespec_test", "*"), Dest: "test.test"},
ExpectedCopiedDirents: []string{"test.test", "test.test/spec.txt", "test.test/spec2.txt"},
},
&testCaseCopy{
FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", `\*globFileSpec_test`, `\*globSpec.txt`), Dest: `\*test.txt`},
false,
[]string{`\*test.txt`},
Spec: FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "f*_test", "*.txt"), Dest: "test.test"},
ExpectedCopiedDirents: []string{"test.test", "test.test/spec.txt", "test.test/spec2.txt"},
},
&testCaseCopy{
FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "file????_test"), Dest: "test"},
true,
[]string{"test", "test/spec.txt", "test/spec2.txt"},
Spec: FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", `\*globFileSpec_test`, `\*globSpec.txt`), Dest: `\*test.test`},
ExpectedCopiedDirents: []string{`\*test.test`},
},
&testCaseCopy{
FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "filespec_test", "????.txt"), Dest: "test.txt"},
false,
[]string{"test.txt"},
Spec: FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "file????_test"), Dest: "test"},
OnlyContents: true,
ExpectedCopiedDirents: []string{"test", "test/spec.txt", "test/spec2.txt"},
},
&testCaseCopy{
FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "file????_test", "????.txt"), Dest: "test.txt"},
false,
[]string{"test.txt"},
Spec: FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "filespec_test", "????.txt"), Dest: "test.test"},
ExpectedCopiedDirents: []string{"test.test"},
},
&testCaseCopy{
FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "filespe[b-d]_test"), Dest: "test"},
true,
[]string{"test", "test/spec.txt", "test/spec2.txt"},
Spec: FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "file????_test", "????.txt"), Dest: "test.test"},
OnlyContents: false,
ExpectedCopiedDirents: []string{"test.test"},
},
&testCaseCopy{
FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "filespe[^d-z]_test"), Dest: "test"},
true,
[]string{"test", "test/spec.txt", "test/spec2.txt"},
Spec: FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "filespe[b-d]_test"), Dest: "test"},
OnlyContents: true,
ExpectedCopiedDirents: []string{"test", "test/spec.txt", "test/spec2.txt"},
},
&testCaseCopy{
FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "filespec_test", "[r-t]pec.txt"), Dest: "test.txt"},
false,
[]string{"test.txt"},
Spec: FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "filespe[^d-z]_test"), Dest: "test"},
OnlyContents: true,
ExpectedCopiedDirents: []string{"test", "test/spec.txt", "test/spec2.txt"},
},
&testCaseCopy{
FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "filespec_test", "[^a-r^t-v]pec.txt"), Dest: "test.txt"},
false,
[]string{"test.txt"},
Spec: FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "filespec_test", "[r-t]pec.txt"), Dest: "test.test"},
OnlyContents: false,
ExpectedCopiedDirents: []string{"test.test"},
},
&testCaseCopy{
FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "filespe[b-d]_test", "[r-t]pec.txt"), Dest: "test.txt"},
false,
[]string{"test.txt"},
Spec: FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "filespec_test", "[^a-r^t-v]pec.txt"), Dest: "test.test"},
ExpectedCopiedDirents: []string{"test.test"},
},
&testCaseCopy{
FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "filespe[^d-z]_test", "[^a-r]pec.txt"), Dest: "test.txt"},
false,
[]string{"test.txt"},
Spec: FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "filespe[b-d]_test", "[r-t]pec.txt"), Dest: "test.test"},
ExpectedCopiedDirents: []string{"test.test"},
},
&testCaseCopy{
Spec: FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "filespe[^d-z]_test", "[^a-r]pec.txt"), Dest: "test.test"},
ExpectedCopiedDirents: []string{"test.test"},
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/util/dirent.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func CopyFile(source string, dest string) error {
// - `cp -r source dest`
//
// When onlyContents = True:
// - dest maxy exist (and must be a dir).
// - dest may exist (and must be a dir).
// - `cp source/* dest/`
func CopyDir(source string, dest string, onlyContents bool) error {
if onlyContents {
Expand Down

0 comments on commit 505c585

Please sign in to comment.