Skip to content

Commit bb9a06f

Browse files
committed
Fix not a repo
1 parent a84dd5c commit bb9a06f

File tree

5 files changed

+207
-23
lines changed

5 files changed

+207
-23
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"editor.codeActionsOnSave": {
99
"source.fixAll": "explicit"
1010
},
11-
"editor.defaultFormatter": "ms-python.black-formatter"
11+
"editor.defaultFormatter": "charliermarsh.ruff"
1212
},
1313
"mypy.runUsingActiveInterpreter": true,
1414
"mypy.targets": ["src", "tests"],

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,11 @@ lines-after-imports = 2 # Ensures consistency for cases when there's variable vs
266266
lines-between-types = 1 # Separate import/from with 1 line
267267

268268
[tool.ruff.per-file-ignores]
269+
# SLF001: Allow private member access in tests
269270
# S101 Allow assert in tests
270271
# S602 Allow shell in test
271272
# T201 Allow print in tests
272-
"tests/**" = ["S101", "S602", "T201"]
273+
"tests/**" = ["SLF001", "S101", "S602", "T201"]
273274

274275
[tool.ruff.pydocstyle]
275276
convention = "pep257"

src/ansible_dev_environment/subcommands/installer.py

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -215,15 +215,16 @@ def _install_galaxy_requirements(self: Installer) -> None:
215215
msg = f"Source installed collections include: {oxford_join(installed)}"
216216
self._output.note(msg)
217217

218-
def _copy_files_using_git_ls_files(
218+
def _find_files_using_git_ls_files(
219219
self: Installer,
220220
local_repo_path: Path | None,
221-
) -> str | None:
221+
) -> tuple[str | None, str | None]:
222222
"""Copy collection files tracked using git ls-files to the build directory.
223223
224224
Args:
225225
local_repo_path: The collection local path.
226226
Returns:
227+
string with the command used to list files or None
227228
string containing a list of files or nothing
228229
"""
229230
msg = "List collection files using git ls-files."
@@ -240,19 +241,21 @@ def _copy_files_using_git_ls_files(
240241
)
241242
except subprocess.CalledProcessError as exc:
242243
err = f"Failed to list collection using git ls-files: {exc} {exc.stderr}"
243-
self._output.critical(err)
244+
self._output.info(err)
245+
return None, None
244246

245-
return tracked_files_output.stdout
247+
return "git ls-files", tracked_files_output.stdout
246248

247-
def _copy_files_using_ls(
249+
def _find_files_using_ls(
248250
self: Installer,
249251
local_repo_path: Path | None,
250-
) -> str | None:
252+
) -> tuple[str | None, str | None]:
251253
"""Copy collection files tracked using ls to the build directory.
252254
253255
Args:
254256
local_repo_path: The collection local path.
255257
Returns:
258+
string with the command used to list files or None
256259
string containing a list of files or nothing
257260
"""
258261
msg = "List collection files using ls."
@@ -269,9 +272,10 @@ def _copy_files_using_ls(
269272
)
270273
except subprocess.CalledProcessError as exc:
271274
err = f"Failed to list collection using ls: {exc} {exc.stderr}"
272-
self._output.critical(err)
275+
self._output.debug(err)
276+
return None, None
273277

274-
return tracked_files_output.stdout
278+
return "ls", tracked_files_output.stdout
275279

276280
def _copy_repo_files(
277281
self: Installer,
@@ -287,35 +291,34 @@ def _copy_repo_files(
287291
"""
288292
if local_repo_path is None:
289293
msg = "Invalid repo path, no files to copy"
290-
self._output.info(msg)
294+
self._output.debug(msg)
291295
return
292296

293297
# Get tracked files from git ls-files command
294-
tracked_files_output = self._copy_files_using_git_ls_files(
298+
found_using, files_stdout = self._find_files_using_git_ls_files(
295299
local_repo_path=local_repo_path,
296300
)
297301

298-
if tracked_files_output is None:
299-
msg = "No tracked files found using git ls-files"
300-
self._output.info(msg)
301-
302-
# If no tracked files found, get files using ls command
303-
tracked_files_output = self._copy_files_using_ls(
302+
if not files_stdout:
303+
found_using, files_stdout = self._find_files_using_ls(
304304
local_repo_path=local_repo_path,
305305
)
306306

307-
if tracked_files_output is None:
308-
msg = "No files found"
309-
self._output.info(msg)
307+
if not files_stdout:
308+
msg = "No files found with either 'git ls-files' or 'ls"
309+
self._output.critical(msg)
310310
return
311311

312+
msg = f"File list generated with '{found_using}'"
313+
self._output.info(msg)
314+
312315
# Parse tracked files output
313-
tracked_files = tracked_files_output.split("\n")
316+
files_list = files_stdout.split("\n")
314317

315318
# Create the destination folder if it doesn't exist
316319
Path(destination_path).mkdir(parents=True, exist_ok=True)
317320

318-
for file in tracked_files:
321+
for file in files_list:
319322
src_file_path = Path(local_repo_path) / file
320323
dest_file_path = Path(destination_path) / file
321324

tests/unit/conftest.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""Fixtures for unit tests."""
2+
3+
from __future__ import annotations
4+
5+
from typing import TYPE_CHECKING
6+
7+
import pytest
8+
9+
from ansible_dev_environment.output import Output
10+
from ansible_dev_environment.utils import TermFeatures
11+
12+
13+
if TYPE_CHECKING:
14+
from pathlib import Path
15+
16+
17+
@pytest.fixture()
18+
def output(tmp_path: Path) -> Output:
19+
"""Create an Output class object as fixture.
20+
21+
:param tmp_path: App configuration object.
22+
"""
23+
return Output(
24+
log_file=str(tmp_path) + "ansible-creator.log",
25+
log_level="notset",
26+
log_append="false",
27+
term_features=TermFeatures(color=False, links=False),
28+
verbosity=0,
29+
)

tests/unit/test_installer.py

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
"""Tests for the installer."""
2+
3+
import subprocess
4+
5+
from argparse import Namespace
6+
from pathlib import Path
7+
8+
import pytest
9+
10+
from ansible_dev_environment.config import Config
11+
from ansible_dev_environment.output import Output
12+
from ansible_dev_environment.subcommands.installer import Installer
13+
14+
15+
NAMESPACE = Namespace()
16+
NAMESPACE.verbose = 0
17+
18+
19+
def test_git_no_files(tmp_path: Path, output: Output) -> None:
20+
"""Test no files using git.
21+
22+
Args:
23+
tmp_dir: Temp directory
24+
output: Output instance
25+
"""
26+
config = Config(args=NAMESPACE, output=output, term_features=output.term_features)
27+
installer = Installer(output=output, config=config)
28+
found_using, files = installer._find_files_using_git_ls_files(local_repo_path=tmp_path)
29+
assert not found_using
30+
assert files is None
31+
32+
33+
def test_git_none_tracked(tmp_path: Path, output: Output) -> None:
34+
"""Test non tracked using git.
35+
36+
Args:
37+
tmp_dir: Temp directory
38+
output: Output instance
39+
"""
40+
config = Config(args=NAMESPACE, output=output, term_features=output.term_features)
41+
installer = Installer(output=output, config=config)
42+
subprocess.run(args=["git", "init"], cwd=tmp_path, check=False)
43+
found_using, files = installer._find_files_using_git_ls_files(local_repo_path=tmp_path)
44+
assert found_using == "git ls-files"
45+
assert files == ""
46+
47+
48+
def test_git_one_tracked(tmp_path: Path, output: Output) -> None:
49+
"""Test one tracked using git.
50+
51+
Args:
52+
tmp_dir: Temp directory
53+
output: Output instance
54+
"""
55+
config = Config(args=NAMESPACE, output=output, term_features=output.term_features)
56+
installer = Installer(output=output, config=config)
57+
subprocess.run(args=["git", "init"], cwd=tmp_path, check=False)
58+
(tmp_path / "file.txt").touch()
59+
subprocess.run(args=["git", "add", "--all"], cwd=tmp_path, check=False)
60+
found_using, files = installer._find_files_using_git_ls_files(local_repo_path=tmp_path)
61+
assert found_using == "git ls-files"
62+
assert files == "file.txt\n"
63+
64+
65+
def test_ls_no_files(tmp_path: Path, output: Output) -> None:
66+
"""Test no files using ls.
67+
68+
Args:
69+
tmp_dir: Temp directory
70+
output: Output instance
71+
"""
72+
config = Config(args=NAMESPACE, output=output, term_features=output.term_features)
73+
installer = Installer(output=output, config=config)
74+
found_using, files = installer._find_files_using_ls(local_repo_path=tmp_path)
75+
assert found_using == "ls"
76+
assert files == ""
77+
78+
79+
def test_ls_one_found(tmp_path: Path, output: Output) -> None:
80+
"""Test one found using ls.
81+
82+
Args:
83+
tmp_dir: Temp directory
84+
output: Output instance
85+
"""
86+
config = Config(args=NAMESPACE, output=output, term_features=output.term_features)
87+
installer = Installer(output=output, config=config)
88+
(tmp_path / "file.txt").touch()
89+
found_using, files = installer._find_files_using_ls(local_repo_path=tmp_path)
90+
assert found_using == "ls"
91+
assert files == "file.txt\n"
92+
93+
94+
def test_copy_no_files(tmp_path: Path, output: Output) -> None:
95+
"""Test file copy no files.
96+
97+
Args:
98+
tmp_dir: Temp directory
99+
output: Output instance
100+
"""
101+
source = tmp_path / "source"
102+
source.mkdir()
103+
dest = tmp_path / "build"
104+
dest.mkdir()
105+
config = Config(args=NAMESPACE, output=output, term_features=output.term_features)
106+
installer = Installer(output=output, config=config)
107+
with pytest.raises(SystemExit) as excinfo:
108+
installer._copy_repo_files(local_repo_path=source, destination_path=dest)
109+
assert excinfo.value.code == 1
110+
111+
112+
def test_copy_using_git(tmp_path: Path, output: Output) -> None:
113+
"""Test file copy using git.
114+
115+
Args:
116+
tmp_dir: Temp directory
117+
output: Output instance
118+
"""
119+
source = tmp_path / "source"
120+
source.mkdir()
121+
dest = tmp_path / "build"
122+
dest.mkdir()
123+
config = Config(args=NAMESPACE, output=output, term_features=output.term_features)
124+
installer = Installer(output=output, config=config)
125+
subprocess.run(args=["git", "init"], cwd=source, check=False)
126+
(source / "file_tracked.txt").touch()
127+
(source / "file_untracked.txt").touch()
128+
subprocess.run(args=["git", "add", "file_tracked.txt"], cwd=source, check=False)
129+
installer._copy_repo_files(local_repo_path=source, destination_path=dest)
130+
moved = dest.glob("**/*")
131+
assert [m.name for m in list(moved)] == ["file_tracked.txt"]
132+
133+
134+
def test_copy_using_ls(tmp_path: Path, output: Output) -> None:
135+
"""Test file copy using ls.
136+
137+
Args:
138+
tmp_dir: Temp directory
139+
output: Output instance
140+
"""
141+
source = tmp_path / "source"
142+
source.mkdir()
143+
dest = tmp_path / "build"
144+
dest.mkdir()
145+
config = Config(args=NAMESPACE, output=output, term_features=output.term_features)
146+
installer = Installer(output=output, config=config)
147+
(source / "file1.txt").touch()
148+
(source / "file2.txt").touch()
149+
installer._copy_repo_files(local_repo_path=source, destination_path=dest)
150+
moved = dest.glob("**/*")
151+
assert [m.name for m in list(moved)] == ["file1.txt", "file2.txt"]

0 commit comments

Comments
 (0)