Skip to content

Commit 3373415

Browse files
authored
Merge pull request #184 from getappmap/default-config_20221020
fix: use default values when config is incomplete
2 parents 96cf4b6 + 8a307a2 commit 3373415

File tree

4 files changed

+68
-8
lines changed

4 files changed

+68
-8
lines changed

appmap.pth

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
import appmap
1+
# The python runtime suppresses errors from import, but we should fail if something goes wrong.
2+
try: import appmap; except Exception: raise

appmap/_implementation/configuration.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,17 @@ def labels(self):
173173

174174
@property
175175
def default(self):
176+
return {"name": self.default_name, "packages": self.default_packages}
177+
178+
@property
179+
def default_name(self):
180+
root_dir = Env.current.root_dir
181+
return default_app_name(root_dir)
182+
183+
@property
184+
def default_packages(self):
176185
root_dir = Env.current.root_dir
177-
return {
178-
"name": default_app_name(root_dir),
179-
"packages": [{"path": p} for p in find_top_packages(root_dir)],
180-
}
186+
return [{"path": p} for p in find_top_packages(root_dir)]
181187

182188
def _load_config(self):
183189
self._config = {"name": None, "packages": []}
@@ -199,6 +205,15 @@ def _load_config(self):
199205
self.file_valid = False
200206
try:
201207
self._config = yaml.safe_load(path.read_text())
208+
if not self._config:
209+
# It parsed, but was (effectively) empty.
210+
self._config = self.default
211+
else:
212+
# It parsed, make sure it has name and packages set.
213+
if not self._config.get("name", None):
214+
self._config["name"] = self.default_name
215+
if not self._config.get("packages", None):
216+
self._config["packages"] = self.default_packages
202217
self.file_valid = True
203218
Env.current.enabled = should_enable
204219
except ParserError:

appmap/test/test_configuration.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Test Configuration"""
22
# pylint: disable=missing-function-docstring
33

4+
from contextlib import contextmanager
45
from distutils.dir_util import copy_tree
56
from pathlib import Path
67

@@ -125,7 +126,7 @@ def test_class_prefix_doesnt_match(self):
125126
assert cf().filter(f) is False
126127

127128

128-
class TestDefaultConfig:
129+
class DefaultHelpers:
129130
def check_default_config(self, expected_name):
130131
assert appmap.enabled()
131132

@@ -137,6 +138,8 @@ def check_default_config(self, expected_name):
137138
{"path": "test"},
138139
]
139140

141+
142+
class TestDefaultConfig(DefaultHelpers):
140143
def test_created(self, git, data_dir, monkeypatch):
141144
repo_root = git.cwd
142145
copy_tree(data_dir / "config", str(repo_root))
@@ -205,3 +208,43 @@ def test_not_created_if_missing_and_not_enabled(self, git, data_dir, monkeypatch
205208

206209
c = Config()
207210
assert not path.is_file()
211+
212+
213+
class TestEmpty(DefaultHelpers):
214+
@pytest.fixture(autouse=True)
215+
def setup_config(self, data_dir, monkeypatch, tmpdir):
216+
copy_tree(data_dir / "config", str(tmpdir))
217+
monkeypatch.chdir(tmpdir)
218+
219+
@contextmanager
220+
def incomplete_config(self):
221+
# pylint: disable=protected-access
222+
with open("appmap-incomplete.yml", mode="w", buffering=1) as f:
223+
print("# Incomplete file", file=f)
224+
yield f
225+
226+
def test_empty(self, tmpdir):
227+
with self.incomplete_config():
228+
appmap._implementation.initialize(
229+
cwd=tmpdir,
230+
env={"APPMAP": "true", "APPMAP_CONFIG": "appmap-incomplete.yml"},
231+
)
232+
self.check_default_config(Path(tmpdir).name)
233+
234+
def test_missing_name(self, tmpdir):
235+
with self.incomplete_config() as f:
236+
print('packages: [{"path": "package"}, {"path": "test"}]', file=f)
237+
appmap._implementation.initialize(
238+
cwd=tmpdir,
239+
env={"APPMAP": "true", "APPMAP_CONFIG": "appmap-incomplete.yml"},
240+
)
241+
self.check_default_config(Path(tmpdir).name)
242+
243+
def test_missing_packages(self, tmpdir):
244+
with self.incomplete_config() as f:
245+
print(f"name: {Path(tmpdir).name}", file=f)
246+
appmap._implementation.initialize(
247+
cwd=tmpdir,
248+
env={"APPMAP": "true", "APPMAP_CONFIG": "appmap-incomplete.yml"},
249+
)
250+
self.check_default_config(Path(tmpdir).name)

ci/run_tests.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#!/usr/bin/env bash
22

33
set -x
4-
docker run -i --rm\
4+
t=$([ -t 0 ] && echo 't')
5+
docker run -i${t} --rm\
56
-v $PWD/dist:/dist -v $PWD/appmap/test/data/unittest:/appmap/test/data/unittest\
67
-v $PWD/ci:/ci\
78
-w /tmp\
8-
python:3.9 bash -ce '/ci/smoketest.sh; /ci/test_pipenv.sh'
9+
python:3.9 bash -ce "${@:-/ci/smoketest.sh; /ci/test_pipenv.sh}"

0 commit comments

Comments
 (0)