Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to Go 1.19 #203

Merged
merged 8 commits into from
Mar 11, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add TestIsValidProjectDir
Signed-off-by: Michael Valdron <mvaldron@redhat.com>
michael-valdron committed Mar 8, 2024

Verified

This commit was signed with the committer’s verified signature.
michael-valdron Michael Valdron
commit d7335ee7c2ee35030af9ec66e9131183b2dfe927
16 changes: 5 additions & 11 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
@@ -855,21 +855,15 @@
// 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

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 {
@@ -1100,7 +1094,7 @@
}
defer resp.Body.Close()

return io.ReadAll(resp.Body)

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

Codecov / codecov/patch

pkg/util/util.go#L1097

Added line #L1097 was not covered by tests
}

// DownloadInMemory uses HTTPRequestParams to download the file and return bytes.
68 changes: 68 additions & 0 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
@@ -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) {

Unchanged files with check annotations Beta

// cleanHttpCache checks cacheDir and deletes all files that were modified more than cacheTime back
func cleanHttpCache(cacheDir string, cacheTime time.Duration) error {
cacheEntries, err := os.ReadDir(cacheDir)

Check warning on line 28 in pkg/util/httpcache.go

Codecov / codecov/patch

pkg/util/httpcache.go#L28

Added line #L28 was not covered by tests
if err != nil {
return err
}
cacheFiles := make([]os.FileInfo, 0, len(cacheEntries))
for _, cacheEntry := range cacheEntries {
info, err := cacheEntry.Info()
if err != nil {
return err
}

Check warning on line 38 in pkg/util/httpcache.go

Codecov / codecov/patch

pkg/util/httpcache.go#L33-L38

Added lines #L33 - L38 were not covered by tests
cacheFiles = append(cacheFiles, info)

Check warning on line 40 in pkg/util/httpcache.go

Codecov / codecov/patch

pkg/util/httpcache.go#L40

Added line #L40 was not covered by tests
}
for _, f := range cacheFiles {