diff --git a/lib/config_test.go b/lib/config_test.go new file mode 100644 index 0000000..a93a2cc --- /dev/null +++ b/lib/config_test.go @@ -0,0 +1,52 @@ +package stew + +import ( + "os" + "path/filepath" + "testing" +) + +func TestGetDefaultStewPath(t *testing.T) { + homeDir, err := os.UserHomeDir() + if err != nil { + t.Errorf("Could not get os.UserHomeDir()") + } + type args struct { + userOS string + } + tests := []struct { + name string + args args + want string + wantErr bool + }{ + { + name: "test1", + args: args{ + userOS: "darwin", + }, + want: filepath.Join(homeDir, ".local", "share", "stew"), + wantErr: false, + }, + { + name: "test2", + args: args{ + userOS: "windows", + }, + want: filepath.Join(homeDir, "AppData", "Local", "stew"), + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := GetDefaultStewPath(tt.args.userOS) + if (err != nil) != tt.wantErr { + t.Errorf("GetDefaultStewPath() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("GetDefaultStewPath() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/lib/stewfile_test.go b/lib/stewfile_test.go index ec60d86..be1e48a 100644 --- a/lib/stewfile_test.go +++ b/lib/stewfile_test.go @@ -293,34 +293,38 @@ func TestNewLockFileDoesntExist(t *testing.T) { } } -// func TestNewSystemInfo(t *testing.T) { -// tests := []struct { -// name string -// }{ -// { -// name: "test1", -// }, -// } -// for _, tt := range tests { -// t.Run(tt.name, func(t *testing.T) { -// tempDir := t.TempDir() -// testSystemInfo := SystemInfo{ -// Os: runtime.GOOS, -// Arch: runtime.GOARCH, -// StewPath: tempDir, -// StewBinPath: filepath.Join(tempDir, "bin"), -// StewPkgPath: filepath.Join(tempDir, "pkg"), -// StewLockFilePath: filepath.Join(tempDir, "Stewfile.lock.json"), -// StewTmpPath: filepath.Join(tempDir, "tmp"), -// } - -// got := NewSystemInfo(tempDir) -// if !reflect.DeepEqual(got, testSystemInfo) { -// t.Errorf("NewSystemInfo() = %v, want %v", got, testSystemInfo) -// } -// }) -// } -// } +func TestNewSystemInfo(t *testing.T) { + tests := []struct { + name string + }{ + { + name: "test1", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tempDir := t.TempDir() + + testStewConfig := StewConfig{ + StewPath: tempDir, + StewBinPath: filepath.Join(tempDir, "bin"), + } + + testSystemInfo := SystemInfo{ + StewPath: tempDir, + StewBinPath: filepath.Join(tempDir, "bin"), + StewPkgPath: filepath.Join(tempDir, "pkg"), + StewLockFilePath: filepath.Join(tempDir, "Stewfile.lock.json"), + StewTmpPath: filepath.Join(tempDir, "tmp"), + } + + got := NewSystemInfo(testStewConfig) + if !reflect.DeepEqual(got, testSystemInfo) { + t.Errorf("NewSystemInfo() = %v, want %v", got, testSystemInfo) + } + }) + } +} func TestDeleteAssetAndBinary(t *testing.T) { tests := []struct { diff --git a/lib/util_test.go b/lib/util_test.go index 34d8cdd..1434abb 100644 --- a/lib/util_test.go +++ b/lib/util_test.go @@ -137,41 +137,6 @@ func TestPathExists(t *testing.T) { } } -// func TestGetStewPath(t *testing.T) { -// tests := []struct { -// name string -// wantErr bool -// }{ -// { -// name: "test1", -// wantErr: false, -// }, -// } -// for _, tt := range tests { -// t.Run(tt.name, func(t *testing.T) { -// homeDir, _ := os.UserHomeDir() -// testStewPath := filepath.Join(homeDir, ".stew") -// stewPathExists, _ := PathExists(testStewPath) -// if !stewPathExists { -// os.MkdirAll(testStewPath, 0755) -// } - -// got, err := GetStewPath() -// if !stewPathExists { -// os.RemoveAll(testStewPath) -// } - -// if (err != nil) != tt.wantErr { -// t.Errorf("GetStewPath() error = %v, wantErr %v", err, tt.wantErr) -// return -// } -// if got != testStewPath { -// t.Errorf("GetStewPath() = %v, want %v", got, testStewPath) -// } -// }) -// } -// } - func TestDownloadFile(t *testing.T) { type args struct { url string @@ -681,120 +646,130 @@ func Test_extractBinary(t *testing.T) { } } -// func TestInstallBinary(t *testing.T) { -// tests := []struct { -// name string -// want string -// wantErr bool -// }{ -// { -// name: "test1", -// want: "ppath", -// wantErr: false, -// }, -// } -// for _, tt := range tests { -// t.Run(tt.name, func(t *testing.T) { -// tempDir := t.TempDir() -// stewPath := filepath.Join(tempDir, ".stew") - -// repo := "ppath" -// systemInfo := NewSystemInfo(stewPath) -// os.MkdirAll(systemInfo.StewBinPath, 0755) -// os.MkdirAll(systemInfo.StewPkgPath, 0755) -// os.MkdirAll(systemInfo.StewTmpPath, 0755) - -// lockFile := LockFile{ -// Os: "darwin", -// Arch: "arm64", -// Packages: []PackageData{ -// { -// Source: "github", -// Owner: "marwanhawari", -// Repo: "ppath", -// Tag: "v0.0.3", -// Asset: "ppath-v0.0.3-darwin-arm64.tar.gz", -// Binary: "ppath", -// URL: "https://github.com/marwanhawari/ppath/releases/download/v0.0.3/ppath-v0.0.3-darwin-arm64.tar.gz", -// }, -// }, -// } - -// downloadedFilePath := filepath.Join(systemInfo.StewPkgPath, "ppath-v0.0.3-darwin-arm64.tar.gz") -// err := DownloadFile(downloadedFilePath, "https://github.com/marwanhawari/ppath/releases/download/v0.0.3/ppath-v0.0.3-darwin-arm64.tar.gz") - -// if err != nil { -// t.Errorf("Could not download file to %v", downloadedFilePath) -// } - -// got, err := InstallBinary(downloadedFilePath, repo, systemInfo, &lockFile, true) -// if (err != nil) != tt.wantErr { -// t.Errorf("InstallBinary() error = %v, wantErr %v", err, tt.wantErr) -// return -// } -// if got != tt.want { -// t.Errorf("InstallBinary() = %v, want %v", got, tt.want) -// } - -// }) -// } -// } - -// func TestInstallBinary_Fail(t *testing.T) { -// tests := []struct { -// name string -// want string -// wantErr bool -// }{ -// { -// name: "test1", -// want: "", -// wantErr: true, -// }, -// } -// for _, tt := range tests { -// t.Run(tt.name, func(t *testing.T) { -// tempDir := t.TempDir() -// stewPath := filepath.Join(tempDir, ".stew") - -// repo := "ppath" -// systemInfo := NewSystemInfo(stewPath) -// os.MkdirAll(systemInfo.StewBinPath, 0755) -// os.MkdirAll(systemInfo.StewPkgPath, 0755) -// os.MkdirAll(systemInfo.StewTmpPath, 0755) - -// lockFile := LockFile{ -// Os: "darwin", -// Arch: "arm64", -// Packages: []PackageData{ -// { -// Source: "github", -// Owner: "marwanhawari", -// Repo: "ppath", -// Tag: "v0.0.3", -// Asset: "ppath-v0.0.3-darwin-arm64.tar.gz", -// Binary: "ppath", -// URL: "https://github.com/marwanhawari/ppath/releases/download/v0.0.3/ppath-v0.0.3-darwin-arm64.tar.gz", -// }, -// }, -// } - -// downloadedFilePath := filepath.Join(systemInfo.StewPkgPath, "ppath-v0.0.3-darwin-arm64.tar.gz") -// err := DownloadFile(downloadedFilePath, "https://github.com/marwanhawari/ppath/releases/download/v0.0.3/ppath-v0.0.3-darwin-arm64.tar.gz") - -// if err != nil { -// t.Errorf("Could not download file to %v", downloadedFilePath) -// } - -// got, err := InstallBinary(downloadedFilePath, repo, systemInfo, &lockFile, false) -// if (err != nil) != tt.wantErr { -// t.Errorf("InstallBinary() error = %v, wantErr %v", err, tt.wantErr) -// return -// } -// if got != tt.want { -// t.Errorf("InstallBinary() = %v, want %v", got, tt.want) -// } - -// }) -// } -// } +func TestInstallBinary(t *testing.T) { + tests := []struct { + name string + want string + wantErr bool + }{ + { + name: "test1", + want: "ppath", + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tempDir := t.TempDir() + + repo := "ppath" + + testStewConfig := StewConfig{ + StewPath: tempDir, + StewBinPath: filepath.Join(tempDir, "bin"), + } + + systemInfo := NewSystemInfo(testStewConfig) + os.MkdirAll(systemInfo.StewBinPath, 0755) + os.MkdirAll(systemInfo.StewPkgPath, 0755) + os.MkdirAll(systemInfo.StewTmpPath, 0755) + + lockFile := LockFile{ + Os: "darwin", + Arch: "arm64", + Packages: []PackageData{ + { + Source: "github", + Owner: "marwanhawari", + Repo: "ppath", + Tag: "v0.0.3", + Asset: "ppath-v0.0.3-darwin-arm64.tar.gz", + Binary: "ppath", + URL: "https://github.com/marwanhawari/ppath/releases/download/v0.0.3/ppath-v0.0.3-darwin-arm64.tar.gz", + }, + }, + } + + downloadedFilePath := filepath.Join(systemInfo.StewPkgPath, "ppath-v0.0.3-darwin-arm64.tar.gz") + err := DownloadFile(downloadedFilePath, "https://github.com/marwanhawari/ppath/releases/download/v0.0.3/ppath-v0.0.3-darwin-arm64.tar.gz") + + if err != nil { + t.Errorf("Could not download file to %v", downloadedFilePath) + } + + got, err := InstallBinary(downloadedFilePath, repo, systemInfo, &lockFile, true) + if (err != nil) != tt.wantErr { + t.Errorf("InstallBinary() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("InstallBinary() = %v, want %v", got, tt.want) + } + + }) + } +} + +func TestInstallBinary_Fail(t *testing.T) { + tests := []struct { + name string + want string + wantErr bool + }{ + { + name: "test1", + want: "", + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tempDir := t.TempDir() + + repo := "ppath" + + testStewConfig := StewConfig{ + StewPath: tempDir, + StewBinPath: filepath.Join(tempDir, "bin"), + } + + systemInfo := NewSystemInfo(testStewConfig) + os.MkdirAll(systemInfo.StewBinPath, 0755) + os.MkdirAll(systemInfo.StewPkgPath, 0755) + os.MkdirAll(systemInfo.StewTmpPath, 0755) + + lockFile := LockFile{ + Os: "darwin", + Arch: "arm64", + Packages: []PackageData{ + { + Source: "github", + Owner: "marwanhawari", + Repo: "ppath", + Tag: "v0.0.3", + Asset: "ppath-v0.0.3-darwin-arm64.tar.gz", + Binary: "ppath", + URL: "https://github.com/marwanhawari/ppath/releases/download/v0.0.3/ppath-v0.0.3-darwin-arm64.tar.gz", + }, + }, + } + + downloadedFilePath := filepath.Join(systemInfo.StewPkgPath, "ppath-v0.0.3-darwin-arm64.tar.gz") + err := DownloadFile(downloadedFilePath, "https://github.com/marwanhawari/ppath/releases/download/v0.0.3/ppath-v0.0.3-darwin-arm64.tar.gz") + + if err != nil { + t.Errorf("Could not download file to %v", downloadedFilePath) + } + + got, err := InstallBinary(downloadedFilePath, repo, systemInfo, &lockFile, false) + if (err != nil) != tt.wantErr { + t.Errorf("InstallBinary() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("InstallBinary() = %v, want %v", got, tt.want) + } + + }) + } +}