summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTristan Van Berkom <tristan.van.berkom@gmail.com>2018-12-26 23:53:29 +0000
committerTristan Van Berkom <tristan.van.berkom@gmail.com>2018-12-26 23:53:29 +0000
commit32a101f67a5a1e751117a1b8976fe72bdd85526f (patch)
tree9993b8eb36d1b71838e245d391d08287c39db0ed
parent94b9948c459fba61d86c336b0eecf3c385c8625e (diff)
parent50c1bb7f88dad806bff703a19dddf11374aac53e (diff)
downloadbuildstream-32a101f67a5a1e751117a1b8976fe72bdd85526f.tar.gz
Merge branch 'tristan/fix-netrc-crasher' into 'master'
Fix netrc crasher See merge request BuildStream/buildstream!1025
-rw-r--r--buildstream/plugins/sources/_downloadablefilesource.py8
-rw-r--r--tests/sources/tar.py16
-rw-r--r--tests/testutils/runcli.py7
3 files changed, 28 insertions, 3 deletions
diff --git a/buildstream/plugins/sources/_downloadablefilesource.py b/buildstream/plugins/sources/_downloadablefilesource.py
index f5c5b3d08..b9b15e268 100644
--- a/buildstream/plugins/sources/_downloadablefilesource.py
+++ b/buildstream/plugins/sources/_downloadablefilesource.py
@@ -231,7 +231,13 @@ class DownloadableFileSource(Source):
if not DownloadableFileSource.__urlopener:
try:
netrc_config = netrc.netrc()
- except FileNotFoundError:
+ except OSError:
+ # If the .netrc file was not found, FileNotFoundError will be
+ # raised, but OSError will be raised directly by the netrc package
+ # in the case that $HOME is not set.
+ #
+ # This will catch both cases.
+ #
DownloadableFileSource.__urlopener = urllib.request.build_opener()
except netrc.NetrcParseError as e:
self.warn('{}: While reading .netrc: {}'.format(self, e))
diff --git a/tests/sources/tar.py b/tests/sources/tar.py
index dc7c85e80..35eb8375e 100644
--- a/tests/sources/tar.py
+++ b/tests/sources/tar.py
@@ -388,3 +388,19 @@ def test_netrc_already_specified_user(cli, datafiles, server_type, tmpdir):
result = cli.run(project=project, args=['source', 'track', 'target.bst'])
result.assert_main_error(ErrorDomain.STREAM, None)
result.assert_task_error(ErrorDomain.SOURCE, None)
+
+
+# Test that BuildStream doesnt crash if HOME is unset while
+# the netrc module is trying to find it's ~/.netrc file.
+@pytest.mark.datafiles(os.path.join(DATA_DIR, 'fetch'))
+def test_homeless_environment(cli, tmpdir, datafiles):
+ project = os.path.join(datafiles.dirname, datafiles.basename)
+ generate_project(project, tmpdir)
+
+ # Create a local tar
+ src_tar = os.path.join(str(tmpdir), "a.tar.gz")
+ _assemble_tar(os.path.join(str(datafiles), "content"), "a", src_tar)
+
+ # Use a track, make sure the plugin tries to find a ~/.netrc
+ result = cli.run(project=project, args=['source', 'track', 'target.bst'], env={'HOME': None})
+ result.assert_success()
diff --git a/tests/testutils/runcli.py b/tests/testutils/runcli.py
index f94cec8ae..1f6951abe 100644
--- a/tests/testutils/runcli.py
+++ b/tests/testutils/runcli.py
@@ -529,13 +529,16 @@ def environment(env):
old_env = {}
for key, value in env.items():
old_env[key] = os.environ.get(key)
- os.environ[key] = value
+ if value is None:
+ os.environ.pop(key, None)
+ else:
+ os.environ[key] = value
yield
for key, value in old_env.items():
if value is None:
- del os.environ[key]
+ os.environ.pop(key, None)
else:
os.environ[key] = value