Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.

Commit 9033715

Browse files
authored
Template directories passed to options are turned into absolute paths (#409)
1 parent e061e16 commit 9033715

3 files changed

Lines changed: 25 additions & 10 deletions

File tree

gapic/generator/options.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
from collections import defaultdict
16+
from os import path
1617
from typing import Any, DefaultDict, Dict, FrozenSet, List, Optional, Tuple
1718

1819
import dataclasses
@@ -95,13 +96,19 @@ def build(cls, opt_string: str) -> 'Options':
9596
# may be our default; perform that replacement.
9697
default_token = 'DEFAULT'
9798
templates = opts.pop('templates', [default_token])
98-
default_path = os.path.realpath(
99-
os.path.join(os.path.dirname(__file__), '..', 'templates'),
100-
)
101-
templates = [
102-
(default_path if path == default_token else path)
103-
for path in templates
104-
]
99+
pwd = path.join(path.dirname(__file__), '..')
100+
default_path = path.realpath(path.join(pwd, 'templates'))
101+
102+
def tweak_path(p):
103+
if p == default_token:
104+
return default_path
105+
106+
if path.isabs(p):
107+
return path.normpath(p)
108+
109+
return path.normpath(path.join(pwd, p))
110+
111+
templates = [tweak_path(p) for p in templates]
105112

106113
retry_cfg = None
107114
retry_paths = opts.pop('retry-config', None)
@@ -121,7 +128,7 @@ def build(cls, opt_string: str) -> 'Options':
121128
for s in sample_paths
122129
for cfg_path in samplegen_utils.generate_all_sample_fpaths(s)
123130
),
124-
templates=tuple(os.path.expanduser(i) for i in templates),
131+
templates=tuple(path.expanduser(i) for i in templates),
125132
lazy_import=bool(opts.pop('lazy-import', False)),
126133
old_naming=bool(opts.pop('old-naming', False)),
127134
)

tests/unit/generator/test_generator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_custom_template_directory():
3636
g = generator.Generator(opts)
3737

3838
# Assert that the Jinja loader will pull from the correct location.
39-
assert g._env.loader.searchpath == ['/templates/']
39+
assert g._env.loader.searchpath == ['/templates']
4040

4141

4242
def test_get_response():

tests/unit/generator/test_options.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import os
1516
import pytest
1617
from unittest import mock
1718
import warnings
@@ -31,7 +32,14 @@ def test_options_empty():
3132
def test_options_replace_templates():
3233
opts = options.Options.build('python-gapic-templates=/foo/')
3334
assert len(opts.templates) == 1
34-
assert opts.templates[0] == '/foo/'
35+
assert opts.templates[0] == '/foo'
36+
37+
38+
def test_options_relative_templates():
39+
opts = options.Options.build('python-gapic-templates=../../squid/clam')
40+
41+
expected = (os.path.abspath('../squid/clam'),)
42+
assert opts.templates == expected
3543

3644

3745
def test_options_unrecognized():

0 commit comments

Comments
 (0)