Skip to content

Commit 07c4aa9

Browse files
authored
Merge pull request #10 from vineelsai26/tests
Tests
2 parents 8b6c2e2 + baefd17 commit 07c4aa9

File tree

11 files changed

+503
-68
lines changed

11 files changed

+503
-68
lines changed

.github/workflows/go.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,45 @@ jobs:
2828

2929
- name: Build for Windows
3030
run: GOOS=windows go build -v ./...
31+
32+
test_linux:
33+
runs-on: ubuntu-latest
34+
needs: build
35+
steps:
36+
- uses: actions/checkout@v4
37+
38+
- name: Set up Go
39+
uses: actions/setup-go@v4
40+
with:
41+
go-version: 1.21
42+
43+
- name: Test
44+
run: go test -timeout 3h -v ./...
45+
46+
test_macos:
47+
runs-on: macos-latest
48+
needs: build
49+
steps:
50+
- uses: actions/checkout@v4
51+
52+
- name: Set up Go
53+
uses: actions/setup-go@v4
54+
with:
55+
go-version: 1.21
56+
57+
- name: Test
58+
run: go test -timeout 3h -v ./...
59+
60+
test_windows:
61+
runs-on: windows-latest
62+
needs: build
63+
steps:
64+
- uses: actions/checkout@v4
65+
66+
- name: Set up Go
67+
uses: actions/setup-go@v4
68+
with:
69+
go-version: 1.21
70+
71+
- name: Test
72+
run: go test -timeout 3h -v ./...

src/node/install.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ func getDownloadURL(version string) (string, error) {
4343
return "", fmt.Errorf("unsupported os or architecture")
4444
}
4545

46-
func installVersion(version string) {
46+
func installVersion(version string) (string, error) {
4747
fullURLFile, err := getDownloadURL(version)
4848
if err != nil {
49-
panic(err)
49+
return "", err
5050
}
5151
downloadDir := filepath.Join(utils.GetHome(), ".cache", "vmn")
5252
downloadedFilePath := filepath.Join(downloadDir, strings.Split(fullURLFile, "/")[len(strings.Split(fullURLFile, "/"))-1])
@@ -56,29 +56,31 @@ func installVersion(version string) {
5656

5757
fileName, err := utils.Download(downloadDir, fullURLFile)
5858
if err != nil {
59-
panic(err)
59+
return "", err
6060
}
6161

6262
// Unzip file
6363
fmt.Println("Installing Node.js version " + version + "...")
6464
if strings.HasSuffix(fileName, ".zip") {
6565
if err := utils.Unzip(downloadedFilePath, utils.GetDestination(version, "node")); err != nil {
66-
panic(err)
66+
return "", err
6767
}
6868
} else if strings.HasSuffix(fileName, ".tar.gz") {
6969
if err := utils.UnGzip(downloadedFilePath, utils.GetDestination(version, "node")); err != nil {
70-
panic(err)
70+
return "", err
7171
}
7272
}
7373

7474
// Delete file
7575
fmt.Println("Cleaning up...")
7676
if err := os.Remove(downloadedFilePath); err != nil {
77-
panic(err)
77+
return "", err
7878
}
79+
80+
return "Node.js version " + version + " installed", nil
7981
}
8082

81-
func Install(version string) {
83+
func Install(version string) (string, error) {
8284
version = strings.TrimPrefix(version, "v")
8385
if version == "latest" {
8486
version = GetLatestVersion()
@@ -95,8 +97,8 @@ func Install(version string) {
9597
}
9698

9799
if utils.IsInstalled(version, "node") {
98-
fmt.Println("Node version " + version + " is already installed")
100+
return "Node version " + version + " is already installed", nil
99101
} else {
100-
installVersion(version)
102+
return installVersion(version)
101103
}
102104
}

src/node/uninstall.go

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,57 @@ package node
33
import (
44
"fmt"
55
"os"
6+
"strings"
67

78
"vineelsai.com/vmn/src/utils"
89
)
910

10-
func uninstallVersion(version string) {
11+
func uninstallVersion(version string) (string, error) {
1112
if !utils.IsInstalled(version, "node") {
1213
panic("Node.js version " + version + " is not installed")
1314
}
1415
fmt.Printf("Uninstalling Node.js %s\n", version)
1516
path, err := utils.GetVersionPath(version, "node")
1617
if err != nil {
17-
panic(err)
18+
return "", err
1819
}
1920
os.RemoveAll(path)
21+
22+
return "Node.js version " + version + " uninstalled successfully", nil
2023
}
2124

22-
func Uninstall(version string) {
25+
func Uninstall(version string) (string, error) {
26+
version = strings.TrimPrefix(version, "v")
2327
if version == "all" {
2428
for _, version := range GetAllVersions() {
25-
uninstallVersion(version)
29+
if _, err := uninstallVersion(version); err != nil {
30+
return "", err
31+
}
2632
}
33+
return "All Node.js versions uninstalled successfully", nil
2734
} else if version == "lts" {
2835
for _, version := range GetAllLTSVersions() {
29-
uninstallVersion(version)
36+
if _, err := uninstallVersion(version); err != nil {
37+
return "", err
38+
}
3039
}
40+
return "All LTS Node.js versions uninstalled successfully", nil
3141
} else if version == "latest" {
32-
uninstallVersion(GetLatestVersion())
42+
version = "v" + GetLatestVersion()
43+
} else if len(strings.Split(version, ".")) == 3 {
44+
version = "v" + version
3345
} else if version != "" {
34-
uninstallVersion(version)
46+
for _, ver := range GetInstalledVersions() {
47+
if strings.HasPrefix(ver, "v"+version) {
48+
if _, err := uninstallVersion(ver); err != nil {
49+
return "", err
50+
}
51+
}
52+
}
53+
return "Node.js version " + version + " uninstalled successfully", nil
3554
} else {
36-
panic("Invalid version")
55+
return "", fmt.Errorf("invalid version")
3756
}
57+
58+
return uninstallVersion(version)
3859
}

src/node/use.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ import (
1010
"vineelsai.com/vmn/src/utils"
1111
)
1212

13-
func useVersion(version string) {
13+
func useVersion(version string) (string, error) {
1414
path, err := utils.GetVersionPath(version, "node")
1515
if err != nil {
16-
panic(err)
16+
return "", err
1717
}
1818

1919
if _, err := os.Stat(filepath.Join(utils.GetHome(), ".vmn")); os.IsNotExist(err) {
2020
if err := os.MkdirAll(filepath.Join(utils.GetHome(), ".vmn"), 0755); err != nil {
21-
panic(err)
21+
return "", err
2222
}
2323
}
2424

2525
f, err := os.OpenFile(filepath.Join(utils.GetHome(), ".vmn", "node-version"), os.O_RDWR|os.O_CREATE, 0755)
2626
if err != nil {
27-
panic(err)
27+
return "", err
2828
}
2929
defer f.Close()
3030

@@ -35,14 +35,15 @@ func useVersion(version string) {
3535
}
3636

3737
if _, err := os.Stat(path); err == nil {
38-
fmt.Println("Setting VMN_VERSION to " + version + " ... ")
3938
setup.SetPath(path)
4039
} else {
41-
fmt.Println("Node.js version " + version + " is not installed")
40+
return "", fmt.Errorf("Node.js version " + version + " is not installed")
4241
}
42+
43+
return version, nil
4344
}
4445

45-
func Use(version string) {
46+
func Use(version string) (string, error) {
4647
version = strings.TrimPrefix(version, "v")
4748
if version == "latest" {
4849
version = GetLatestVersion()
@@ -62,5 +63,5 @@ func Use(version string) {
6263
fmt.Println("installing node version " + version + "...")
6364
installVersion(version)
6465
}
65-
useVersion(version)
66+
return useVersion(version)
6667
}

src/python/install.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"vineelsai.com/vmn/src/utils"
1212
)
1313

14-
func installVersion(version string) {
14+
func installVersion(version string) (string, error) {
1515
fullURLFile := "https://www.python.org/ftp/python/" + version + "/Python-" + version + ".tgz"
1616
downloadDir := filepath.Join(utils.GetHome(), ".cache", "vmn")
1717
buildDir := filepath.Join(downloadDir, "build", version)
@@ -21,14 +21,14 @@ func installVersion(version string) {
2121
fmt.Println("Downloading Python from " + fullURLFile)
2222
fileName, err := utils.Download(downloadDir, fullURLFile)
2323
if err != nil {
24-
panic(err)
24+
return "", err
2525
}
2626

2727
// Unzip file
2828
fmt.Println("Building Python version " + version + " from source...")
2929
if strings.HasSuffix(fileName, ".tgz") {
3030
if err := utils.UnGzip(downloadedFilePath, buildDir); err != nil {
31-
panic(err)
31+
return "", err
3232
}
3333

3434
cmd := exec.Command(
@@ -38,11 +38,11 @@ func installVersion(version string) {
3838
)
3939
out, err := cmd.StdoutPipe()
4040
if err != nil {
41-
panic(err)
41+
return "", err
4242
}
4343

4444
if err = cmd.Start(); err != nil {
45-
panic(err)
45+
return "", err
4646
}
4747
for {
4848
tmp := make([]byte, 1024)
@@ -57,14 +57,16 @@ func installVersion(version string) {
5757
// Delete file
5858
fmt.Println("Cleaning up...")
5959
if err := os.Remove(downloadedFilePath); err != nil {
60-
panic(err)
60+
return "", err
6161
}
6262
if err := os.RemoveAll(buildDir); err != nil {
63-
panic(err)
63+
exec.Command("/bin/bash", "-c", "rm -rf "+buildDir).Run()
6464
}
65+
66+
return "Python version " + version + " installed successfully", nil
6567
}
6668

67-
func Install(version string) {
69+
func Install(version string) (string, error) {
6870
version = strings.TrimPrefix(version, "v")
6971
if version == "latest" {
7072
version = GetLatestVersion()
@@ -75,12 +77,12 @@ func Install(version string) {
7577
} else if len(strings.Split(version, ".")) == 1 {
7678
version = GetLatestVersionOfVersion(version, "")
7779
} else {
78-
panic("invalid version")
80+
return "", fmt.Errorf("invalid version")
7981
}
8082

8183
if utils.IsInstalled(version, "python") {
82-
fmt.Println("Python version " + version + " is already installed")
84+
return "Python version " + version + " is already installed", nil
8385
} else {
84-
installVersion(version)
86+
return installVersion(version)
8587
}
8688
}

src/python/uninstall.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,50 @@ package python
33
import (
44
"fmt"
55
"os"
6+
"strings"
67

78
"vineelsai.com/vmn/src/utils"
89
)
910

10-
func uninstallVersion(version string) {
11+
func uninstallVersion(version string) (string, error) {
1112
if !utils.IsInstalled(version, "python") {
12-
panic("Python version " + version + " is not installed")
13+
return "", fmt.Errorf("Python version " + version + " is not installed")
1314
}
1415
fmt.Printf("Uninstalling Python %s\n", version)
1516
path, err := utils.GetVersionPath(version, "python")
1617
if err != nil {
17-
panic(err)
18+
return "", err
1819
}
1920
os.RemoveAll(path)
21+
22+
return "Python version " + version + " uninstalled successfully", nil
2023
}
2124

22-
func Uninstall(version string) {
25+
func Uninstall(version string) (string, error) {
26+
version = strings.TrimPrefix(version, "v")
2327
if version == "all" {
2428
for _, version := range GetAllVersions() {
25-
uninstallVersion(version)
29+
if _, err := uninstallVersion(version); err != nil {
30+
return "", err
31+
}
2632
}
33+
return "All Python versions uninstalled successfully", nil
2734
} else if version == "latest" {
28-
uninstallVersion(GetLatestVersion())
35+
version = "v" + GetLatestVersion()
36+
} else if len(strings.Split(version, ".")) == 3 {
37+
version = "v" + version
2938
} else if version != "" {
30-
uninstallVersion(version)
39+
for _, ver := range GetInstalledVersions() {
40+
if strings.HasPrefix(ver, version) {
41+
version = "v" + ver
42+
if _, err := uninstallVersion(version); err != nil {
43+
return "", err
44+
}
45+
}
46+
}
47+
return "Python version " + version + " uninstalled successfully", nil
3148
} else {
32-
panic("Invalid version")
49+
return "", fmt.Errorf("invalid version")
3350
}
51+
return uninstallVersion(version)
3452
}

0 commit comments

Comments
 (0)