Skip to content

Commit

Permalink
path lib to filepath lib
Browse files Browse the repository at this point in the history
  • Loading branch information
marwanhawari committed Mar 5, 2022
1 parent 1c86269 commit 62e9c91
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 50 deletions.
4 changes: 2 additions & 2 deletions cmd/browse.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"fmt"
"os"
"path"
"path/filepath"

"github.com/marwanhawari/stew/constants"
stew "github.com/marwanhawari/stew/lib"
Expand Down Expand Up @@ -57,7 +57,7 @@ func Browse(cliInput string) {
assetIndex, _ := stew.Contains(releaseAssets, asset)

downloadURL := githubProject.Releases[tagIndex].Assets[assetIndex].DownloadURL
downloadPath := path.Join(stewPkgPath, asset)
downloadPath := filepath.Join(stewPkgPath, asset)
downloadPathExists, err := stew.PathExists(downloadPath)
stew.CatchAndExit(err)

Expand Down
4 changes: 2 additions & 2 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"

"github.com/marwanhawari/stew/constants"
Expand Down Expand Up @@ -102,7 +102,7 @@ func Install(cliInputs []string) {
fmt.Println(constants.GreenColor(asset))
}

downloadPath := path.Join(stewPkgPath, asset)
downloadPath := filepath.Join(stewPkgPath, asset)
downloadPathExists, err := stew.PathExists(downloadPath)
stew.CatchAndExit(err)
if downloadPathExists {
Expand Down
4 changes: 2 additions & 2 deletions cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"fmt"
"os"
"path"
"path/filepath"

"github.com/marwanhawari/stew/constants"
stew "github.com/marwanhawari/stew/lib"
Expand Down Expand Up @@ -89,7 +89,7 @@ func Upgrade(cliFlag bool, binaryName string) {

downloadURL := githubProject.Releases[tagIndex].Assets[assetIndex].DownloadURL

downloadPath := path.Join(stewPkgPath, asset)
downloadPath := filepath.Join(stewPkgPath, asset)
downloadPathExists, err := stew.PathExists(downloadPath)
stew.CatchAndExit(err)
if downloadPathExists {
Expand Down
14 changes: 7 additions & 7 deletions lib/stewfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"

"github.com/marwanhawari/stew/constants"
)
Expand Down Expand Up @@ -131,10 +131,10 @@ type SystemInfo struct {
func NewSystemInfo(stewPath string) SystemInfo {
var systemInfo SystemInfo
systemInfo.StewPath = stewPath
systemInfo.StewBinPath = path.Join(stewPath, "bin")
systemInfo.StewPkgPath = path.Join(stewPath, "pkg")
systemInfo.StewLockFilePath = path.Join(stewPath, "Stewfile.lock.json")
systemInfo.StewTmpPath = path.Join(stewPath, "tmp")
systemInfo.StewBinPath = filepath.Join(stewPath, "bin")
systemInfo.StewPkgPath = filepath.Join(stewPath, "pkg")
systemInfo.StewLockFilePath = filepath.Join(stewPath, "Stewfile.lock.json")
systemInfo.StewTmpPath = filepath.Join(stewPath, "tmp")
systemInfo.Os = getOS()
systemInfo.Arch = getArch()

Expand All @@ -143,8 +143,8 @@ func NewSystemInfo(stewPath string) SystemInfo {

// DeleteAssetAndBinary will delete the asset from the ~/.stew/pkg path and delete the binary from the ~/.stew/bin path
func DeleteAssetAndBinary(stewPkgPath, stewBinPath, asset, binary string) error {
assetPath := path.Join(stewPkgPath, asset)
binPath := path.Join(stewBinPath, binary)
assetPath := filepath.Join(stewPkgPath, asset)
binPath := filepath.Join(stewBinPath, binary)
err := os.RemoveAll(assetPath)
if err != nil {
return err
Expand Down
30 changes: 15 additions & 15 deletions lib/stewfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package stew
import (
"io/ioutil"
"os"
"path"
"path/filepath"
"reflect"
"runtime"
"testing"
Expand Down Expand Up @@ -69,7 +69,7 @@ func Test_readLockFileJSON(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tempDir := t.TempDir()
lockFilePath := path.Join(tempDir, "Stewfile.lock.json")
lockFilePath := filepath.Join(tempDir, "Stewfile.lock.json")
WriteLockFileJSON(testLockfile, lockFilePath)

got, err := readLockFileJSON(lockFilePath)
Expand Down Expand Up @@ -97,7 +97,7 @@ func TestWriteLockFileJSON(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tempDir := t.TempDir()
lockFilePath := path.Join(tempDir, "Stewfile.lock.json")
lockFilePath := filepath.Join(tempDir, "Stewfile.lock.json")

if err := WriteLockFileJSON(testLockfile, lockFilePath); (err != nil) != tt.wantErr {
t.Errorf("WriteLockFileJSON() error = %v, wantErr %v", err, tt.wantErr)
Expand Down Expand Up @@ -201,7 +201,7 @@ func TestReadStewfileContents(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {

tempDir := t.TempDir()
testStewfilePath := path.Join(tempDir, "Stewfile")
testStewfilePath := filepath.Join(tempDir, "Stewfile")
ioutil.WriteFile(testStewfilePath, []byte(testStewfileContents), 0644)

got, err := ReadStewfileContents(testStewfilePath)
Expand Down Expand Up @@ -241,7 +241,7 @@ func TestNewLockFile(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {

tempDir := t.TempDir()
lockFilePath := path.Join(tempDir, "Stewfile.lock.json")
lockFilePath := filepath.Join(tempDir, "Stewfile.lock.json")
WriteLockFileJSON(testLockfile, lockFilePath)

got, err := NewLockFile(lockFilePath, tt.args.userOS, tt.args.userArch)
Expand Down Expand Up @@ -279,7 +279,7 @@ func TestNewLockFileDoesntExist(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {

tempDir := t.TempDir()
lockFilePath := path.Join(tempDir, "Stewfile.lock.json")
lockFilePath := filepath.Join(tempDir, "Stewfile.lock.json")

got, err := NewLockFile(lockFilePath, tt.args.userOS, tt.args.userArch)
if (err != nil) != tt.wantErr {
Expand Down Expand Up @@ -309,10 +309,10 @@ func TestNewSystemInfo(t *testing.T) {
Os: runtime.GOOS,
Arch: runtime.GOARCH,
StewPath: tempDir,
StewBinPath: path.Join(tempDir, "bin"),
StewPkgPath: path.Join(tempDir, "pkg"),
StewLockFilePath: path.Join(tempDir, "Stewfile.lock.json"),
StewTmpPath: path.Join(tempDir, "tmp"),
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)
Expand All @@ -336,10 +336,10 @@ func TestDeleteAssetAndBinary(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tempDir := t.TempDir()
os.MkdirAll(path.Join(tempDir, "pkg"), 0755)
testStewAssetPath := path.Join(tempDir, "pkg", "testAsset.tar.gz")
os.MkdirAll(path.Join(tempDir, "bin"), 0755)
testStewBinaryPath := path.Join(tempDir, "bin", "testBinary")
os.MkdirAll(filepath.Join(tempDir, "pkg"), 0755)
testStewAssetPath := filepath.Join(tempDir, "pkg", "testAsset.tar.gz")
os.MkdirAll(filepath.Join(tempDir, "bin"), 0755)
testStewBinaryPath := filepath.Join(tempDir, "bin", "testBinary")

ioutil.WriteFile(testStewAssetPath, []byte("This is a test asset"), 0644)
ioutil.WriteFile(testStewBinaryPath, []byte("This is a test binary"), 0644)
Expand All @@ -351,7 +351,7 @@ func TestDeleteAssetAndBinary(t *testing.T) {
t.Errorf("Either the asset or the binary does not exist yet")
}

if err := DeleteAssetAndBinary(path.Dir(testStewAssetPath), path.Dir(testStewBinaryPath), path.Base(testStewAssetPath), path.Base(testStewBinaryPath)); (err != nil) != tt.wantErr {
if err := DeleteAssetAndBinary(filepath.Dir(testStewAssetPath), filepath.Dir(testStewBinaryPath), filepath.Base(testStewAssetPath), filepath.Base(testStewBinaryPath)); (err != nil) != tt.wantErr {
t.Errorf("DeleteAssetAndBinary() error = %v, wantErr %v", err, tt.wantErr)
}

Expand Down
44 changes: 22 additions & 22 deletions lib/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package stew
import (
"io/ioutil"
"os"
"path"
"path/filepath"
"reflect"
"runtime"
"testing"
Expand Down Expand Up @@ -72,7 +72,7 @@ func Test_isExecutableFile(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tempDir := t.TempDir()
testExecutableFilePath := path.Join(tempDir, tt.args.filePath)
testExecutableFilePath := filepath.Join(tempDir, tt.args.filePath)
if tt.want {
ioutil.WriteFile(testExecutableFilePath, []byte("An executable file"), 0755)
} else {
Expand Down Expand Up @@ -121,7 +121,7 @@ func TestPathExists(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tempDir := t.TempDir()
testFilePath := path.Join(tempDir, tt.args.path)
testFilePath := filepath.Join(tempDir, tt.args.path)
if tt.want {
ioutil.WriteFile(testFilePath, []byte("A test file"), 0644)
}
Expand Down Expand Up @@ -151,7 +151,7 @@ func TestGetStewPath(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
homeDir, _ := os.UserHomeDir()
testStewPath := path.Join(homeDir, ".stew")
testStewPath := filepath.Join(homeDir, ".stew")
stewPathExists, _ := PathExists(testStewPath)
if !stewPathExists {
os.MkdirAll(testStewPath, 0755)
Expand Down Expand Up @@ -200,7 +200,7 @@ func TestDownloadFile(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tempDir := t.TempDir()
testDownloadPath := path.Join(tempDir, path.Base(tt.args.url))
testDownloadPath := filepath.Join(tempDir, filepath.Base(tt.args.url))
if err := DownloadFile(testDownloadPath, tt.args.url); (err != nil) != tt.wantErr {
t.Errorf("DownloadFile() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down Expand Up @@ -235,8 +235,8 @@ func Test_copyFile(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tempDir := t.TempDir()
srcFilePath := path.Join(tempDir, tt.args.srcFile)
destFilePath := path.Join(tempDir, tt.args.destFile)
srcFilePath := filepath.Join(tempDir, tt.args.srcFile)
destFilePath := filepath.Join(tempDir, tt.args.destFile)

ioutil.WriteFile(srcFilePath, []byte("A test file"), 0644)

Expand Down Expand Up @@ -280,11 +280,11 @@ func Test_walkDir(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
tempDir := t.TempDir()

ioutil.WriteFile(path.Join(tempDir, "testFile.txt"), []byte("A test file"), 0644)
os.MkdirAll(path.Join(tempDir, "bin"), 0755)
ioutil.WriteFile(path.Join(tempDir, "bin", "binDirTestFile.txt"), []byte("Another test file"), 0644)
ioutil.WriteFile(filepath.Join(tempDir, "testFile.txt"), []byte("A test file"), 0644)
os.MkdirAll(filepath.Join(tempDir, "bin"), 0755)
ioutil.WriteFile(filepath.Join(tempDir, "bin", "binDirTestFile.txt"), []byte("Another test file"), 0644)

want := []string{path.Join(tempDir, "bin", "binDirTestFile.txt"), path.Join(tempDir, "testFile.txt")}
want := []string{filepath.Join(tempDir, "bin", "binDirTestFile.txt"), filepath.Join(tempDir, "testFile.txt")}

got, err := walkDir(tempDir)
if (err != nil) != tt.wantErr {
Expand Down Expand Up @@ -337,15 +337,15 @@ func Test_getBinary(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
tempDir := t.TempDir()

testBinaryFilePath := path.Join(tempDir, tt.binaryName)
testBinaryFilePath := filepath.Join(tempDir, tt.binaryName)
ioutil.WriteFile(testBinaryFilePath, []byte("An executable file"), 0755)
testNonBinaryFilePath := path.Join(tempDir, "testNonBinary")
testNonBinaryFilePath := filepath.Join(tempDir, "testNonBinary")
ioutil.WriteFile(testNonBinaryFilePath, []byte("Not an executable file"), 0644)

testFilePaths := []string{testBinaryFilePath, testNonBinaryFilePath}

wantBinaryFile := path.Join(tempDir, tt.binaryName)
wantBinaryName := path.Base(wantBinaryFile)
wantBinaryFile := filepath.Join(tempDir, tt.binaryName)
wantBinaryName := filepath.Base(wantBinaryFile)

got, got1, err := getBinary(testFilePaths, tt.args.repo)
if (err != nil) != tt.wantErr {
Expand Down Expand Up @@ -385,7 +385,7 @@ func Test_getBinaryError(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
tempDir := t.TempDir()

testNonBinaryFilePath := path.Join(tempDir, "testNonBinary")
testNonBinaryFilePath := filepath.Join(tempDir, "testNonBinary")
ioutil.WriteFile(testNonBinaryFilePath, []byte("Not an executable file"), 0644)

testFilePaths := []string{testNonBinaryFilePath}
Expand Down Expand Up @@ -702,8 +702,8 @@ func Test_extractBinary(t *testing.T) {
{
name: "test1",
args: args{
downloadedFilePath: path.Join(t.TempDir(), "ppath-v0.0.3-darwin-arm64.tar.gz"),
tmpExtractionPath: path.Join(t.TempDir(), "tmp"),
downloadedFilePath: filepath.Join(t.TempDir(), "ppath-v0.0.3-darwin-arm64.tar.gz"),
tmpExtractionPath: filepath.Join(t.TempDir(), "tmp"),
},
url: "https://github.com/marwanhawari/ppath/releases/download/v0.0.3/ppath-v0.0.3-darwin-arm64.tar.gz",
wantErr: false,
Expand Down Expand Up @@ -735,7 +735,7 @@ func TestInstallBinary(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tempDir := t.TempDir()
stewPath := path.Join(tempDir, ".stew")
stewPath := filepath.Join(tempDir, ".stew")

repo := "ppath"
systemInfo := NewSystemInfo(stewPath)
Expand All @@ -759,7 +759,7 @@ func TestInstallBinary(t *testing.T) {
},
}

downloadedFilePath := path.Join(systemInfo.StewPkgPath, "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 {
Expand Down Expand Up @@ -794,7 +794,7 @@ func TestInstallBinary_Fail(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tempDir := t.TempDir()
stewPath := path.Join(tempDir, ".stew")
stewPath := filepath.Join(tempDir, ".stew")

repo := "ppath"
systemInfo := NewSystemInfo(stewPath)
Expand All @@ -818,7 +818,7 @@ func TestInstallBinary_Fail(t *testing.T) {
},
}

downloadedFilePath := path.Join(systemInfo.StewPkgPath, "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 {
Expand Down

0 comments on commit 62e9c91

Please sign in to comment.