Merge pull request #1454 from apache/tristan/remove-required-global-fixture

Remove requirement of running private global fixtures
diff --git a/src/buildstream/_stream.py b/src/buildstream/_stream.py
index 004a35d..2645171 100644
--- a/src/buildstream/_stream.py
+++ b/src/buildstream/_stream.py
@@ -51,7 +51,8 @@
 from ._state import State
 from .types import _KeyStrength, _PipelineSelection, _Scope, _HostMount
 from .plugin import Plugin
-from . import utils, _yaml, _site, _pipeline
+from . import utils, node, _yaml, _site, _pipeline
+from .downloadablefilesource import DownloadableFileSource
 
 
 # Stream()
@@ -115,6 +116,16 @@
         # Reset the element loader state
         Element._reset_load_state()
 
+        # Reset global state in node.pyx, this is for the sake of
+        # test isolation.
+        node._reset_global_state()
+
+        # Ensure that any global state loaded by the downloadablefilesource
+        # is discarded in between sessions (different invocations of the CLI
+        # may come with different local state such as .netrc files, so we need
+        # a reset here).
+        DownloadableFileSource._reset_url_opener()
+
     # set_project()
     #
     # Set the top-level project.
diff --git a/src/buildstream/testing/_fixtures.py b/src/buildstream/testing/_fixtures.py
index 095a5f6..927954d 100644
--- a/src/buildstream/testing/_fixtures.py
+++ b/src/buildstream/testing/_fixtures.py
@@ -21,8 +21,6 @@
 import psutil
 import pytest
 
-from buildstream import node, DownloadableFileSource
-
 
 # Number of seconds to wait for background threads to exit.
 _AWAIT_THREADS_TIMEOUT_SECONDS = 5
@@ -54,10 +52,3 @@
     assert has_no_unexpected_background_threads(default_thread_number)
     yield
     assert has_no_unexpected_background_threads(default_thread_number)
-
-
-# Reset global state in node.pyx to improve test isolation
-@pytest.fixture(autouse=True)
-def reset_global_node_state():
-    node._reset_global_state()
-    DownloadableFileSource._reset_url_opener()
diff --git a/src/buildstream/testing/_sourcetests/conftest.py b/src/buildstream/testing/_sourcetests/conftest.py
index 6790712..fead43a 100644
--- a/src/buildstream/testing/_sourcetests/conftest.py
+++ b/src/buildstream/testing/_sourcetests/conftest.py
@@ -14,4 +14,4 @@
 #  You should have received a copy of the GNU Lesser General Public
 #  License along with this library. If not, see <http://www.gnu.org/licenses/>.
 
-from .._fixtures import reset_global_node_state, default_thread_number, thread_check  # pylint: disable=unused-import
+from .._fixtures import default_thread_number, thread_check  # pylint: disable=unused-import
diff --git a/tests/conftest.py b/tests/conftest.py
index d79ad40..e3b13c9 100755
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -27,7 +27,6 @@
 from buildstream.testing import register_repo_kind, sourcetests_collection_hook
 from buildstream.testing._fixtures import (  # pylint: disable=unused-import
     default_thread_number,
-    reset_global_node_state,
     thread_check,
 )
 from buildstream.testing.integration import integration_cache  # pylint: disable=unused-import
diff --git a/tests/sourcecache/fetch.py b/tests/sourcecache/fetch.py
index f37242f..26f808d 100644
--- a/tests/sourcecache/fetch.py
+++ b/tests/sourcecache/fetch.py
@@ -151,6 +151,9 @@
             assert ("SUCCESS Fetching from {}".format(repo.source_config(ref=ref)["url"])) in res.stderr
 
             # Check that the source in both in the source dir and the local CAS
+            project = Project(project_dir, context)
+            project.ensure_fully_loaded()
+
             element = project.load_elements([element_name])[0]
             element._query_source_cache()
             assert element._cached_sources()