Skip to content

Commit a383c60

Browse files
authored
Merge pull request #18 from DogFortune/issue/16-create-test
テストの追加
2 parents 80f656c + 5f06828 commit a383c60

File tree

6 files changed

+249
-13
lines changed

6 files changed

+249
-13
lines changed

.github/workflows/dataset-update.yml

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,34 @@ on:
99
workflow_dispatch:
1010

1111
permissions:
12+
checks: write
1213
contents: write
1314
pull-requests: write
1415

1516
jobs:
17+
test:
18+
runs-on: ubuntu-24.04
19+
timeout-minutes: 5
20+
steps:
21+
- uses: actions/checkout@v6
22+
- uses: actions/setup-python@v6
23+
with:
24+
python-version: "3.14"
25+
cache: "pipenv"
26+
- name: Install pipenv
27+
run: |
28+
python -m pip install --upgrade pip
29+
python -m pip install pipenv
30+
- name: Install packages
31+
run: pipenv sync --dev
32+
- name: Run pytest
33+
run: pipenv run pytest -q --junit-xml pytest.xml
34+
continue-on-error: true
35+
- name: Publish Test Report
36+
uses: mikepenz/action-junit-report@v5
37+
if: success() || failure()
38+
with:
39+
report_paths: "pytest.xml"
1640
update-dataset:
1741
runs-on: ubuntu-24.04
1842
timeout-minutes: 5
@@ -31,7 +55,7 @@ jobs:
3155
with:
3256
python-version: "3.14"
3357
- name: Update Dataset
34-
run: python scripts/main.py
58+
run: python scripts/app/main.py
3559
- name: Create Pull Request
3660
uses: peter-evans/create-pull-request@v8
3761
with:

Pipfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ name = "pypi"
66
[packages]
77

88
[dev-packages]
9+
pytest = "*"
10+
black = "*"
911

1012
[requires]
1113
python_version = "3.14"

Pipfile.lock

Lines changed: 158 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ exclude = '''
66
| \.venv
77
)/
88
'''
9+
[tool.pytest.ini_options]
10+
pythonpath = "scripts"
11+
testpaths = ["tests"]

scripts/main.py renamed to scripts/app/main.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
from pathlib import Path
44

55

6-
def __load_json(path: str):
6+
def load_json(path: str):
77
with open(path) as f:
88
raw_json = json.load(f)
99

1010
return raw_json
1111

1212

13-
def create_file(file_data: dict, is_ext: bool, verbose: bool):
13+
def create_file(
14+
file_data: dict, is_ext: bool, verbose: bool, current: Path = Path("files")
15+
):
1416
"""ファイルを作成します
1517
1618
:param file_data: テーマから抽出したファイル情報
@@ -20,12 +22,11 @@ def create_file(file_data: dict, is_ext: bool, verbose: bool):
2022
:param verbose: 詳細表示か否か
2123
:type verbose: bool
2224
"""
23-
current = Path("files")
2425
if not current.is_dir():
2526
current.mkdir()
26-
for key in file_data.keys():
27+
for key, value in file_data.items():
2728
file_path = (
28-
current.joinpath(f"DummyName.{key}") if is_ext else current.joinpath(key)
29+
current.joinpath(f"{value}.{key}") if is_ext else current.joinpath(key)
2930
)
3031
if file_path.is_file():
3132
if verbose:
@@ -37,15 +38,14 @@ def create_file(file_data: dict, is_ext: bool, verbose: bool):
3738
print(f"create: {str(file_path)}")
3839

3940

40-
def create_folder(folder_data: dict, verbose: bool):
41+
def create_folder(folder_data: dict, verbose: bool, current: Path = Path("folders")):
4142
"""フォルダを作成します。フォルダだけだとgit管理に含まれないのでからのファイルをフォルダ内に作っておきます。
4243
4344
:param folder_info_json: _description_
4445
:type folder_info_json: dict
4546
:param verbose: 詳細表示か否か
4647
:type verbose: bool
4748
"""
48-
current = Path("folders")
4949
if not current.is_dir():
5050
current.mkdir()
5151
for folder_name in folder_data.keys():
@@ -73,13 +73,13 @@ def main(args=None):
7373

7474
verbose = args.verbose
7575

76-
file_data = __load_json("./manifests/files.json")
76+
file_data = load_json("./manifests/files.json")
7777
create_file(file_data, False, verbose)
7878

79-
ext_data = __load_json("./manifests/extensions.json")
79+
ext_data = load_json("./manifests/extensions.json")
8080
create_file(ext_data, True, verbose)
8181

82-
folder_data = __load_json("./manifests/folders.json")
82+
folder_data = load_json("./manifests/folders.json")
8383
create_folder(folder_data, verbose)
8484

8585

tests/test_main.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from app import main
2+
from tempfile import TemporaryDirectory
3+
from pathlib import Path
4+
import pytest
5+
6+
7+
@pytest.mark.parametrize(
8+
["data", "is_ext", "expected_file_name"],
9+
[
10+
pytest.param({".pug-lintrc": "pug"}, False, ".pug-lintrc"),
11+
pytest.param({"png": "image"}, True, "image.png"),
12+
pytest.param({"key": "value"}, True, "value.key"),
13+
],
14+
)
15+
def test_create_file(data, is_ext, expected_file_name):
16+
"""渡したデータから、想定している名前のファイルが生成されている事
17+
file.json -> ファイル名は{key}になる事
18+
extensions.json -> ファイル名は{value}.{key}になる事
19+
20+
:param data: _description_
21+
:type data: _type_
22+
:param is_ext: _description_
23+
:type is_ext: bool
24+
:param expected_file_name: _description_
25+
:type expected_file_name: _type_
26+
"""
27+
with TemporaryDirectory() as dir:
28+
current = Path(dir)
29+
main.create_file(data, is_ext, False, current)
30+
31+
result_file = list(current.iterdir())
32+
33+
assert len(result_file) == 1
34+
assert result_file[0].name == expected_file_name
35+
36+
37+
@pytest.mark.parametrize(
38+
["data", "expected_folder_name"],
39+
[
40+
pytest.param({"rust": "folder-rust"}, "rust"),
41+
],
42+
)
43+
def test_create_folder(data, expected_folder_name):
44+
with TemporaryDirectory() as dir:
45+
current = Path(dir)
46+
main.create_folder(data, False, current)
47+
48+
result_folder = list(current.iterdir())
49+
print(result_folder)
50+
assert len(result_folder) == 1
51+
assert result_folder[0].name == expected_folder_name

0 commit comments

Comments
 (0)