|
| 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