Skip to content

Commit

Permalink
fix file path on windows (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
koxudaxi authored Dec 27, 2020
1 parent b09bd1a commit 93a008c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 16 deletions.
16 changes: 10 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,18 @@ jobs:
./scripts/lint.sh
pip install isort==5.6.4
./scripts/lint.sh
- name: Unit Test
if: matrix.os != 'windows-latest'
- name: Unit Test on Linux
if: matrix.os == 'ubuntu-latest'
run: |
./scripts/test.sh
# - name: Unit Test windows
# if: matrix.os == 'windows-latest'
# run: |
# ./scripts/test.bat
- name: Unit Test on macOS
if: matrix.os == 'macos-latest'
run: |
./scripts/test.sh
- name: Unit Test on Windows
if: matrix.os == 'windows-latest'
run: |
./scripts/test.bat
- name: Upload coverage to Codecov
if: matrix.os == 'ubuntu-latest'
uses: codecov/codecov-action@v1
Expand Down
5 changes: 4 additions & 1 deletion datamodel_code_generator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,10 @@ def generate(
if output.suffix:
raise Error('Modular references require an output directory, not a file')
modules = {
output.joinpath(*name): (result.body, str(result.source or input_filename))
output.joinpath(*name): (
result.body,
str(result.source.as_posix() if result.source else input_filename),
)
for name, result in sorted(results.items())
}

Expand Down
2 changes: 1 addition & 1 deletion datamodel_code_generator/parser/jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,7 @@ def parse_raw(self) -> None:
):
self.current_source_path = Path()
self.model_resolver.after_load_files = {
str(s.path) for s in self.iter_source
s.path.as_posix() for s in self.iter_source
}
for source in self.iter_source:
if self.current_source_path is not None:
Expand Down
6 changes: 4 additions & 2 deletions tests/model/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ class {{ class_name }}:


def test_template_base():
with NamedTemporaryFile('w') as dummy_template:
with NamedTemporaryFile('w', delete=False) as dummy_template:
dummy_template.write('abc')
dummy_template.seek(0)
dummy_template.close()
a: TemplateBase = A(Path(dummy_template.name))
assert str(a.template_file_path) == dummy_template.name
assert a._render() == 'abc'
Expand All @@ -63,9 +64,10 @@ def test_data_model():
name='a', data_type=DataType(type='str'), default="" 'abc' "", required=True
)

with NamedTemporaryFile('w') as dummy_template:
with NamedTemporaryFile('w', delete=False) as dummy_template:
dummy_template.write(template)
dummy_template.seek(0)
dummy_template.close()
B.TEMPLATE_FILE_PATH = dummy_template.name
data_model = B(
name='test_model',
Expand Down
7 changes: 6 additions & 1 deletion tests/parser/test_openapi.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import platform
from pathlib import Path
from typing import List, Optional

Expand Down Expand Up @@ -351,7 +352,11 @@ def test_openapi_parser_parse_allof():

def test_openapi_parser_parse_alias():
parser = OpenAPIParser(Path(DATA_PATH / 'alias.yaml'),)
results = {'/'.join(p): r for p, r in parser.parse().items()}
if platform.system() == 'Windows':
delimiter = '\\'
else:
delimiter = '/'
results = {delimiter.join(p): r for p, r in parser.parse().items()}
openapi_parser_parse_alias_dir = (
EXPECTED_OPEN_API_PATH / 'openapi_parser_parse_alias'
)
Expand Down
18 changes: 13 additions & 5 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import platform
import shutil
from pathlib import Path
from tempfile import TemporaryDirectory
Expand Down Expand Up @@ -484,21 +485,28 @@ def test_main_custom_template_dir(capsys: CaptureFixture) -> None:

@freeze_time('2019-07-26')
def test_pyproject():
if platform.system() == 'Windows':
get_path = lambda path: str(path).replace('\\', '\\\\')
else:
get_path = lambda path: str(path)
with TemporaryDirectory() as output_dir:
output_dir = Path(output_dir)

with chdir(output_dir):
output_file: Path = output_dir / 'output.py'
pyproject_toml_path = Path(DATA_PATH) / "project" / "pyproject.toml"
pyproject_toml = (
pyproject_toml_path.read_text()
.replace('INPUT_PATH', str(OPEN_API_DATA_PATH / 'api.yaml'))
.replace('OUTPUT_PATH', str(output_file))
.replace('ALIASES_PATH', str(OPEN_API_DATA_PATH / 'empty_aliases.json'))
.replace('INPUT_PATH', get_path(OPEN_API_DATA_PATH / 'api.yaml'))
.replace('OUTPUT_PATH', get_path(output_file))
.replace(
'ALIASES_PATH', get_path(OPEN_API_DATA_PATH / 'empty_aliases.json')
)
.replace(
'EXTRA_TEMPLATE_DATA_PATH',
str(OPEN_API_DATA_PATH / 'empty_data.json'),
get_path(OPEN_API_DATA_PATH / 'empty_data.json'),
)
.replace('CUSTOM_TEMPLATE_DIR_PATH', str(output_dir))
.replace('CUSTOM_TEMPLATE_DIR_PATH', get_path(output_dir))
)
(output_dir / 'pyproject.toml').write_text(pyproject_toml)

Expand Down

0 comments on commit 93a008c

Please sign in to comment.