Skip to content

Commit

Permalink
add TestIsValidProjectDir
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Valdron <[email protected]>
  • Loading branch information
michael-valdron committed Mar 8, 2024
1 parent 0444b09 commit d7335ee
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 11 deletions.
16 changes: 5 additions & 11 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -855,21 +855,15 @@ func FilterIgnores(filesChanged, filesDeleted, absIgnoreRules []string) (filesCh
// IsValidProjectDir checks that the folder to download the project from devfile is
// either empty or only contains the devfile used.
func IsValidProjectDir(path string, devfilePath string) error {
fileEntries, err := os.ReadDir(path)
return isValidProjectDirOnFS(path, devfilePath, filesystem.DefaultFs{})

Check warning on line 858 in pkg/util/util.go

View check run for this annotation

Codecov / codecov/patch

pkg/util/util.go#L858

Added line #L858 was not covered by tests
}

func isValidProjectDirOnFS(path string, devfilePath string, fs filesystem.Filesystem) error {
files, err := fs.ReadDir(path)
if err != nil {
return err
}

files := make([]os.FileInfo, 0, len(fileEntries))
for _, fileEntry := range fileEntries {
info, err := fileEntry.Info()
if err != nil {
return err
}

files = append(files, info)
}

if len(files) > 1 {
return errors.Errorf("Folder %s is not empty. It can only contain the devfile used.", path)
} else if len(files) == 1 {
Expand Down
68 changes: 68 additions & 0 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,74 @@ func TestFilterIgnores(t *testing.T) {
}
}

func TestIsValidProjectDir(t *testing.T) {
const validProjectDirPath = "/projectDirs/validProjectDir"
const emptyProjectDirPath = "/projectDirs/emptyProjectDir"
const invalidProjectDirWithFiles = "/projectDirs/invalidProjectDirWithFiles"
const invalidProjectDirWithSubDirPath = "/projectDirs/invalidProjectDirWithSubDir"
fs := filesystem.NewFakeFs()
tests := []struct {
name string
path string
devfilePath string
isDevfilePathDir bool
otherFiles []string
wantErr bool
}{
{
name: "Case 1: Valid project directory",
path: validProjectDirPath,
devfilePath: "devfile.yaml",
},
{
name: "Case 2: Valid empty project directory",
path: emptyProjectDirPath,
},
{
name: "Case 3: Invalid project directory with files",
path: invalidProjectDirWithFiles,
devfilePath: "devfile.yaml",
otherFiles: []string{"package.json", "app.js"},
wantErr: true,
},
{
name: "Case 4: Invalid project directory with subdirectory",
path: invalidProjectDirWithSubDirPath,
devfilePath: "devfile",
isDevfilePathDir: true,
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create test projectDir
fs.MkdirAll(tt.path, os.ModePerm)

// create devfile (or subdir)
if tt.devfilePath != "" {
if tt.isDevfilePathDir {
fs.MkdirAll(filepath.Join(tt.path, tt.devfilePath), os.ModePerm)
} else {
fs.Create(filepath.Join(tt.path, tt.devfilePath))
}
}

// create other files
for _, otherFile := range tt.otherFiles {
fs.Create(filepath.Join(tt.path, otherFile))
}

err := isValidProjectDirOnFS(tt.path, tt.devfilePath, fs)
if !tt.wantErr && err != nil {
t.Errorf("Got unexpected error: %v", err)
} else if tt.wantErr && err == nil {
t.Errorf("Expected an error but got nil")
}
})
}
}

func TestDownloadFile(t *testing.T) {
// Start a local HTTP server
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
Expand Down

0 comments on commit d7335ee

Please sign in to comment.