Skip to content

Commit

Permalink
Reworded documentation, fixed multiple targets to 1 existing file bug…
Browse files Browse the repository at this point in the history
…, added a testcase for it.
  • Loading branch information
OliverLok committed Dec 3, 2024
1 parent 9fbb434 commit ba1adf1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
4 changes: 2 additions & 2 deletions docs/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -785,8 +785,8 @@ All types of FileSpecs share some common fields:

A FileSpec with `type` equal to `path` points to an absolute or relative path accessible from the current machine.
The path may also include a glob pattern to target multiple files or directories.
For glob patterns resolving to multiple files, files will be copied into the specified `dest`.
If `dest` does not exist, it will be automatically created.
For paths that match multiple dirents, the dirents will be copied into `dest`.
If `dest` does not exist, it will be automatically created as a directory.
When a relative path is specified, additional context is required to know the relative base.
For example, a FileSpec in an assignment config is relative to the assignment directory (the directory where the `assignment.json` file lives).

Expand Down
9 changes: 6 additions & 3 deletions internal/common/filespec.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func (this *FileSpec) CopyTarget(baseDir string, destDir string, onlyContents bo
// no-op.
return nil
case FILESPEC_TYPE_PATH:
return this.matchAndCopyPaths(baseDir, destDir, onlyContents)
return this.copyPaths(baseDir, destDir, onlyContents)
case FILESPEC_TYPE_GIT:
return this.copyGit(destDir)
case FILESPEC_TYPE_URL:
Expand All @@ -235,7 +235,7 @@ func (this *FileSpec) CopyTarget(baseDir string, destDir string, onlyContents bo
}
}

func (this *FileSpec) matchAndCopyPaths(baseDir string, destDir string, onlyContents bool) error {
func (this *FileSpec) copyPaths(baseDir string, destDir string, onlyContents bool) error {
if !this.IsPath() {
return fmt.Errorf("Cannot match targets: FileSpec must be a path.")
}
Expand Down Expand Up @@ -270,7 +270,10 @@ func (this *FileSpec) matchAndCopyPaths(baseDir string, destDir string, onlyCont
return fmt.Errorf("No targets found for the path '%s'.", this.Path)
}

// Create a new directory if multiple paths are matched.
if util.IsFile(destPath) && len(paths) > 1 {
return fmt.Errorf("Cannot copy multiple targets into the existing file '%s'.", destDir)
}

if !util.PathExists(destPath) && len(paths) > 1 {
err := util.MkDir(destPath)
if err != nil {
Expand Down
18 changes: 17 additions & 1 deletion internal/common/filespec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type testCaseCopy struct {
Spec FileSpec
OnlyContents bool
ExpectedCopiedDirents []string
ExpectErrorForSameDir bool
}

func TestFileSpecCopy(test *testing.T) {
Expand All @@ -88,10 +89,20 @@ func TestFileSpecCopy(test *testing.T) {

destDir := filepath.Join(tempDir, "dest")

if testCase.ExpectErrorForSameDir {
destDir = filepath.Dir(testCase.Spec.Path)
}

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

if testCase.ExpectErrorForSameDir {
continue
}

dirents, err := util.GetAllDirents(destDir)
Expand Down Expand Up @@ -170,6 +181,11 @@ func getCopyTestCases() []*testCaseCopy {
Spec: FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "filespec_test", "*"), Dest: "test"},
ExpectedCopiedDirents: []string{"test", "test/spec.txt", "test/spec2.txt"},
},
&testCaseCopy{
Spec: FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "filespec_test", "*"), Dest: "spec.txt"},
OnlyContents: true,
ExpectErrorForSameDir: true,
},
&testCaseCopy{
Spec: FileSpec{Type: "path", Path: filepath.Join(config.GetTestdataDir(), "files", "filespec_test", "*.txt"), Dest: "test"},
ExpectedCopiedDirents: []string{"test", "test/spec.txt", "test/spec2.txt"},
Expand Down

0 comments on commit ba1adf1

Please sign in to comment.