← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~cjwatson/launchpad:fix-lpcraft-load-interface into launchpad:master

 

Colin Watson has proposed merging ~cjwatson/launchpad:fix-lpcraft-load-interface into launchpad:master.

Commit message:
Make load_configuration interface more useful

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/+git/launchpad/+merge/414604

The Launchpad web application typically won't have a `.launchpad.yaml` file on disk, so an interface that requires opening it as a file isn't so useful.  Adjust this to accept anything that `yaml.safe_load` accepts instead, i.e. a stream or a string.
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:fix-lpcraft-load-interface into launchpad:master.
diff --git a/lib/lp/code/model/lpcraft.py b/lib/lp/code/model/lpcraft.py
index 67ee176..02124f9 100644
--- a/lib/lp/code/model/lpcraft.py
+++ b/lib/lp/code/model/lpcraft.py
@@ -37,10 +37,13 @@ def _expand_job_values(values):
 
 
 def load_configuration(configuration_file):
-    """loads a `.launchpad.yaml` file into a `Configuration` object"""
+    """Loads a `.launchpad.yaml` file into a `Configuration` object.
+
+    :param configuration_file: Anything that you can pass to `yaml.safe_load`,
+        i.e. a stream or a string containing `.launchpad.yaml` content.
+    """
     # load yaml
-    with open(configuration_file) as stream:
-        content = yaml.safe_load(stream)
+    content = yaml.safe_load(configuration_file)
     # expand matrix
     expanded_values = content.copy()
     if expanded_values.get("jobs"):
diff --git a/lib/lp/code/model/tests/test_lpcraft.py b/lib/lp/code/model/tests/test_lpcraft.py
index e1663ab..7e29755 100644
--- a/lib/lp/code/model/tests/test_lpcraft.py
+++ b/lib/lp/code/model/tests/test_lpcraft.py
@@ -1,7 +1,6 @@
 # Copyright 2022 Canonical Ltd.  This software is licensed under the
 # GNU Affero General Public License version 3 (see the file LICENSE).
 
-import os
 from textwrap import dedent
 
 from lp.code.model.lpcraft import load_configuration
@@ -9,19 +8,13 @@ from lp.testing import TestCase
 
 
 class TestLoadConfiguration(TestCase):
-    def create_configuration_file(self, s):
-        path = os.path.join(self.makeTemporaryDirectory(), "launchpad.yaml")
-        with open(path, "w") as f:
-            f.write(s)
-        return path
-
     def test_load_configuration_with_pipeline(self):
         c = dedent("""\
         pipeline:
             - test
         """)
 
-        configuration = load_configuration(self.create_configuration_file(c))
+        configuration = load_configuration(c)
 
         self.assertEqual(
             ["test"], configuration.pipeline
@@ -43,7 +36,7 @@ class TestLoadConfiguration(TestCase):
                     focal
         """)
 
-        configuration = load_configuration(self.create_configuration_file(c))
+        configuration = load_configuration(c)
 
         self.assertEqual(
             ["test"], configuration.pipeline,
@@ -71,7 +64,7 @@ class TestLoadConfiguration(TestCase):
                       architectures: [amd64, s390x]
         """)
 
-        configuration = load_configuration(self.create_configuration_file(c))
+        configuration = load_configuration(c)
 
         self.assertEqual(
             ["test"], configuration.pipeline,