Skip to content

Commit d38eee7

Browse files
committed
Add CLI tests for Go
1 parent 33324cf commit d38eee7

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed

tests/package_managers/test_go.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,17 @@ class GoProject:
1717
"""
1818
A representation of a Go project and its local environment.
1919
"""
20-
def __init__(self, go_dir: str, directory: str, env: dict):
20+
def __init__(self, go_dir: Path, directory: Path, env: dict):
2121
self.go_dir = go_dir
2222
self.directory = directory
2323
self.env = env
2424

25-
2625
def get_go_dir_contents(self) -> str:
2726
"""
2827
List the contents in the go directory.
2928
30-
Note that this only looks at the name and path of the filesm ignoring
31-
their actual contents!
29+
Note that this only looks at the name and path of the files, ignoring
30+
their actual contents.
3231
"""
3332
h = hashlib.new("md5")
3433
for root, _, files in os.walk(self.go_dir):
@@ -65,7 +64,13 @@ def test_go_no_change(new_go_project):
6564
Test that certain `go` commands relied on by Supply-Chain Firewall
6665
not to error or modify the local installation state indeed have these properties.
6766
"""
68-
def _test_go_no_change(project: GoProject, base_go: GoProject, local_init_state: str, global_init_state: str, command: list) -> bool:
67+
def _test_go_no_change(
68+
project: GoProject,
69+
base_go: GoProject,
70+
local_init_state: str,
71+
global_init_state: str,
72+
command: list
73+
) -> bool:
6974
"""
7075
Tests that a given Poetry command does not encounter any errors and does not
7176
modify the local installation state when run in the context of a given project.
@@ -76,16 +81,19 @@ def _test_go_no_change(project: GoProject, base_go: GoProject, local_init_state:
7681
return go_show(project) == local_init_state and base_go.get_go_dir_contents() == global_init_state
7782

7883
test_cases = []
79-
for command in ["build", "generate", "get", "install", "mod", "run"]:
80-
for param in ["-h", "-help"]:
84+
for command in {"build", "generate", "get", "install", "mod", "run"}:
85+
for param in {"-h", "-help"}:
8186
test_cases.append(["go", command, param])
8287

83-
base_go = GoProject(get_gopath(), "", {})
88+
base_go = GoProject(get_gopath(), Path(""), {})
8489
global_init_state = base_go.get_go_dir_contents()
8590

8691
local_init_state = go_show(new_go_project)
8792

88-
assert all(_test_go_no_change(new_go_project, base_go, local_init_state, global_init_state, command) for command in test_cases)
93+
assert all(
94+
_test_go_no_change(new_go_project, base_go, local_init_state, global_init_state, command)
95+
for command in test_cases
96+
)
8997

9098

9199
def go_show(project: GoProject) -> str:
@@ -103,15 +111,15 @@ def go_show(project: GoProject) -> str:
103111
return go_show.stdout.lower()
104112

105113

106-
def get_gopath() -> str:
114+
def get_gopath() -> Path:
107115
"""
108116
Retrieve the default path where go install packages.
109117
"""
110118
gopath = subprocess.run(["go", "env", "GOPATH"], check=True, text=True, capture_output=True)
111-
return gopath.stdout.strip()
119+
return Path(gopath.stdout.strip())
112120

113121

114-
def _init_go_env(directory) -> (Path, dict):
122+
def _init_go_env(directory) -> tuple[Path, dict]:
115123
"""
116124
Initialize a fresh Go environment in `directory` and return both the path
117125
used by go and the environment variables that should be provided to

tests/package_managers/test_go_class.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import subprocess
1111
import textwrap
1212

13-
from .test_go import GoProject, get_gopath, go_show, new_go_project
13+
from .test_go import GoProject, get_gopath, go_show
1414

1515
PACKAGE_MANAGER = Go()
1616
"""
@@ -33,7 +33,7 @@ def test_go_command_would_install_remote(new_go_project):
3333
["go", "run", f"{TARGET}@latest"],
3434
]
3535

36-
base_go = GoProject(get_gopath(), "", {})
36+
base_go = GoProject(get_gopath(), Path(""), {})
3737
global_init_state = base_go.get_go_dir_contents()
3838

3939
local_init_state = go_show(new_go_project)
@@ -64,7 +64,7 @@ def test_go_command_would_install_local(new_go_project):
6464
["go", "install", "."],
6565
]
6666

67-
base_go = GoProject(get_gopath(), "", {})
67+
base_go = GoProject(get_gopath(), Path(""), {})
6868
global_init_state = base_go.get_go_dir_contents()
6969

7070
_install_target(new_go_project)

tests/test_cli.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_cli_incorrect_subcommand():
3636

3737
@pytest.mark.parametrize(
3838
"target",
39-
["npm", "pip", "poetry"]
39+
["go", "npm", "pip", "poetry"]
4040
)
4141
def test_cli_audit_basic_usage(target: str):
4242
"""
@@ -83,7 +83,7 @@ def test_cli_audit_unknown_package_manager():
8383

8484
@pytest.mark.parametrize(
8585
"target",
86-
["npm", "pip", "poetry"]
86+
["go", "npm", "pip", "poetry"]
8787
)
8888
def test_cli_audit_all_options_package_manager(target: str):
8989
"""
@@ -113,6 +113,7 @@ def test_cli_configure_basic_usage():
113113
assert args.log_level == _DEFAULT_LOG_LEVEL
114114

115115
assert not args.remove
116+
assert not args.alias_go
116117
assert not args.alias_npm
117118
assert not args.alias_pip
118119
assert not args.alias_poetry
@@ -125,6 +126,7 @@ def test_cli_configure_basic_usage():
125126
@pytest.mark.parametrize(
126127
"option",
127128
[
129+
["--alias-go"],
128130
["--alias-npm"],
129131
["--alias-pip"],
130132
["--alias-poetry"],
@@ -146,6 +148,7 @@ def test_cli_configure_removal(option: list[str]):
146148
@pytest.mark.parametrize(
147149
"command",
148150
[
151+
["go", "get", "github.com/aws/aws-sdk-go-v2"],
149152
["npm", "install", "react"],
150153
["pip", "install", "requests"],
151154
["poetry", "add", "requests"],
@@ -182,6 +185,7 @@ def test_cli_run_all_options_no_command():
182185
@pytest.mark.parametrize(
183186
"command",
184187
[
188+
["go", "get", "github.com/aws/aws-sdk-go-v2"],
185189
["npm", "install", "react"],
186190
["pip", "install", "requests"],
187191
["poetry", "add", "requests"],
@@ -209,6 +213,7 @@ def test_cli_run_all_options_command(command: list[str]):
209213
@pytest.mark.parametrize(
210214
"command",
211215
[
216+
["go", "get", "github.com/aws/aws-sdk-go-v2"],
212217
["npm", "install", "react"],
213218
["pip", "install", "requests"],
214219
["poetry", "install", "requests"],
@@ -235,10 +240,16 @@ def test_cli_run_package_manager_dry_run(command: list[str]):
235240
@pytest.mark.parametrize(
236241
"target,test",
237242
[
243+
("go", "npm"),
244+
("go", "pip"),
245+
("go", "poetry"),
246+
("npm", "go"),
238247
("npm", "pip"),
239248
("npm", "poetry"),
249+
("pip", "go"),
240250
("pip", "npm"),
241251
("pip", "poetry"),
252+
("poetry", "go"),
242253
("poetry", "npm"),
243254
("poetry", "pip"),
244255
]

0 commit comments

Comments
 (0)