File tree Expand file tree Collapse file tree 2 files changed +36
-1
lines changed
Expand file tree Collapse file tree 2 files changed +36
-1
lines changed Original file line number Diff line number Diff line change 1+ import json
2+ import mimetypes
13from pathlib import Path
24from typing import Dict , List , Optional
35
@@ -35,6 +37,10 @@ class Config(BaseModel):
3537 @staticmethod
3638 def load_from_path (path : Path ) -> "Config" :
3739 """Creates a Config from provided JSON or YAML file and sets a bunch of globals from it"""
38- config_data = yaml .safe_load (path .read_text ())
40+ mime = mimetypes .guess_type (path .as_uri (), strict = True )[0 ]
41+ if mime == "application/json" :
42+ config_data = json .loads (path .read_text ())
43+ else :
44+ config_data = yaml .safe_load (path .read_text ())
3945 config = Config (** config_data )
4046 return config
Original file line number Diff line number Diff line change 1+ import json
12import pathlib
23
4+ import pytest
5+ import yaml
6+
37from openapi_python_client .config import Config
48
59
@@ -28,3 +32,28 @@ def test_load_from_path(mocker):
2832 assert config .project_name_override == "project-name"
2933 assert config .package_name_override == "package_name"
3034 assert config .package_version_override == "package_version"
35+
36+
37+ DATA = {"class_overrides" : {"Class1" : {"class_name" : "ExampleClass" , "module_name" : "example_module" }}}
38+
39+
40+ def json_with_tabs (d ):
41+ return json .dumps (d , indent = 4 ).replace (" " , "\t " )
42+
43+
44+ @pytest .mark .parametrize (
45+ "filename,dump" ,
46+ [
47+ ("example.yml" , yaml .dump ),
48+ ("example.json" , json .dumps ),
49+ ("example.yaml" , yaml .dump ),
50+ ("example.json" , json_with_tabs ),
51+ ],
52+ )
53+ def test_load_filenames (tmp_path , filename , dump ):
54+ yml_file = tmp_path .joinpath (filename )
55+ with open (yml_file , "w" ) as f :
56+ f .write (dump (DATA ))
57+
58+ config = Config .load_from_path (yml_file )
59+ assert config .class_overrides == DATA ["class_overrides" ]
You can’t perform that action at this time.
0 commit comments