This is an automated email from the ASF dual-hosted git repository.

tvb pushed a commit to branch tristan/fix-list-content-symlinks
in repository https://gitbox.apache.org/repos/asf/buildstream.git

commit 45479c4f7285412ca0e9b0852764d703a8f58880
Author: Tristan van Berkom <[email protected]>
AuthorDate: Fri Sep 27 11:55:29 2024 +0900

    tests/frontend/artifact_list_contents.py: Add coverage for symlinks
    
    This will protect against regressions around listing symlinks.
    
    This patch adds a separate project subdir in tests/frontend, instead of
    continuing the bad practice of having all these tests share the test
    data in tests/frontend/project.
---
 tests/frontend/artifact_list_contents.py           | 89 ++++++++++++++--------
 .../artifact_list_contents/elements/import-bin.bst |  4 +
 .../elements/import-links.bst                      |  4 +
 .../artifact_list_contents/elements/target.bst     |  8 ++
 .../files/bin-files/usr/bin/hello                  |  3 +
 .../files/files-and-links/basicfile                |  1 +
 .../files/files-and-links/basicfolder/subdir-file  |  0
 tests/frontend/artifact_list_contents/project.conf | 10 +++
 8 files changed, 88 insertions(+), 31 deletions(-)

diff --git a/tests/frontend/artifact_list_contents.py 
b/tests/frontend/artifact_list_contents.py
index e4611313c..3796323d5 100644
--- a/tests/frontend/artifact_list_contents.py
+++ b/tests/frontend/artifact_list_contents.py
@@ -25,28 +25,42 @@ from buildstream.exceptions import ErrorDomain
 # Project directory
 DATA_DIR = os.path.join(
     os.path.dirname(os.path.realpath(__file__)),
-    "project",
+    "artifact_list_contents",
 )
 
 
+def prepare_symlink(project):
+    # Create the link before running the tests.
+    # This is needed for users working on Windows, git checks out symlinks as 
files which content is the name
+    # of the symlink and the test therefore doesn't have the correct content
+    os.symlink(
+        os.path.join("..", "basicfile"),
+        os.path.join(project, "files", "files-and-links", "basicfolder", 
"basicsymlink"),
+    )
+
+
 @pytest.mark.datafiles(DATA_DIR)
 @pytest.mark.parametrize("target", ["element-name", "artifact-name"])
 @pytest.mark.parametrize("with_project", [True, False], ids=["with-project", 
"without-project"])
 def test_artifact_list_exact_contents(cli, datafiles, target, with_project):
     project = str(datafiles)
+    prepare_symlink(project)
 
     # Get the cache key of our test element
-    key = cli.get_element_key(project, "import-bin.bst")
+    key = cli.get_element_key(project, "target.bst")
 
     # Ensure we have an artifact to read
-    result = cli.run(project=project, args=["build", "import-bin.bst"])
+    result = cli.run(project=project, args=["build", "target.bst"])
     result.assert_success()
 
     if target == "element-name":
-        arg = "import-bin.bst"
+        arg_bin = "import-bin.bst"
+        arg_links = "import-links.bst"
     elif target == "artifact-name":
-        key = cli.get_element_key(project, "import-bin.bst")
-        arg = "test/import-bin/" + key
+        key_bin = cli.get_element_key(project, "import-bin.bst")
+        key_links = cli.get_element_key(project, "import-links.bst")
+        arg_bin = "test/import-bin/" + key_bin
+        arg_links = "test/import-links/" + key_links
     else:
         assert False, "unreachable"
 
@@ -54,18 +68,21 @@ def test_artifact_list_exact_contents(cli, datafiles, 
target, with_project):
     if not with_project:
         os.remove(os.path.join(project, "project.conf"))
 
-    # List the contents via the key
-    result = cli.run(project=project, args=["artifact", "list-contents", arg])
+    expected_output_bin = ("{target}:\n" "\tusr\n" "\tusr/bin\n" 
"\tusr/bin/hello\n\n").format(target=arg_bin)
+    expected_output_links = (
+        "{target}:\n" "\tbasicfile\n" "\tbasicfolder\n" 
"\tbasicfolder/basicsymlink\n" "\tbasicfolder/subdir-file\n\n"
+    ).format(target=arg_links)
 
-    # Expect to fail if we try to list by element name and there is no project
-    if target == "element-name" and not with_project:
-        result.assert_main_error(ErrorDomain.STREAM, "project-not-loaded")
-    else:
-        result.assert_success()
+    for arg, expected_output in [(arg_bin, expected_output_bin), (arg_links, 
expected_output_links)]:
+        # List the contents via the key
+        result = cli.run(project=project, args=["artifact", "list-contents", 
arg])
 
-        expected_output_template = 
"{target}:\n\tusr\n\tusr/bin\n\tusr/bin/hello\n\n"
-        expected_output = expected_output_template.format(target=arg)
-        assert expected_output in result.output
+        # Expect to fail if we try to list by element name and there is no 
project
+        if target == "element-name" and not with_project:
+            result.assert_main_error(ErrorDomain.STREAM, "project-not-loaded")
+        else:
+            result.assert_success()
+            assert expected_output in result.output
 
 
 # NOTE: The pytest-datafiles package has an issue where it fails to transfer 
any
@@ -81,36 +98,48 @@ def test_artifact_list_exact_contents(cli, datafiles, 
target, with_project):
 @pytest.mark.parametrize("target", ["element-name", "artifact-name"])
 def test_artifact_list_exact_contents_long(cli, datafiles, target):
     project = str(datafiles)
+    prepare_symlink(project)
 
     # Ensure we have an artifact to read
-    result = cli.run(project=project, args=["build", "import-bin.bst"])
+    result = cli.run(project=project, args=["build", "target.bst"])
     assert result.exit_code == 0
 
     if target == "element-name":
-        arg = "import-bin.bst"
+        arg_bin = "import-bin.bst"
+        arg_links = "import-links.bst"
     elif target == "artifact-name":
-        key = cli.get_element_key(project, "import-bin.bst")
-        arg = "test/import-bin/" + key
+        key_bin = cli.get_element_key(project, "import-bin.bst")
+        key_links = cli.get_element_key(project, "import-links.bst")
+        arg_bin = "test/import-bin/" + key_bin
+        arg_links = "test/import-links/" + key_links
     else:
         assert False, "unreachable"
 
-    # List the contents via the element name
-    result = cli.run(project=project, args=["artifact", "list-contents", 
"--long", arg])
-    assert result.exit_code == 0
-    expected_output_template = (
+    expected_output_bin = (
         "{target}:\n"
         "\tdrwxr-xr-x  dir    0           usr\n"
         "\tdrwxr-xr-x  dir    0           usr/bin\n"
         "\t-rwxr-xr-x  exe    28          usr/bin/hello\n\n"
-    )
-    expected_output = expected_output_template.format(target=arg)
+    ).format(target=arg_bin)
+    expected_output_links = (
+        "{target}:\n"
+        "\t-rw-r--r--  reg    14          basicfile\n"
+        "\tdrwxr-xr-x  dir    0           basicfolder\n"
+        "\tlrwxrwxrwx  link   12          basicfolder/basicsymlink -> 
../basicfile\n"
+        "\t-rw-r--r--  reg    0           basicfolder/subdir-file\n\n"
+    ).format(target=arg_links)
 
-    assert expected_output in result.output
+    # List the contents via the element name
+    for arg, expected_output in [(arg_bin, expected_output_bin), (arg_links, 
expected_output_links)]:
+        result = cli.run(project=project, args=["artifact", "list-contents", 
"--long", arg])
+        assert result.exit_code == 0
+        assert expected_output in result.output
 
 
 @pytest.mark.datafiles(DATA_DIR)
 def test_artifact_list_exact_contents_glob(cli, datafiles):
     project = str(datafiles)
+    prepare_symlink(project)
 
     # Ensure we have an artifact to read
     result = cli.run(project=project, args=["build", "target.bst"])
@@ -122,14 +151,12 @@ def test_artifact_list_exact_contents_glob(cli, 
datafiles):
 
     # get the cahe keys for each element in the glob
     import_bin_key = cli.get_element_key(project, "import-bin.bst")
-    import_dev_key = cli.get_element_key(project, "import-dev.bst")
-    compose_all_key = cli.get_element_key(project, "compose-all.bst")
+    import_links_key = cli.get_element_key(project, "import-links.bst")
     target_key = cli.get_element_key(project, "target.bst")
 
     expected_artifacts = [
         "test/import-bin/" + import_bin_key,
-        "test/import-dev/" + import_dev_key,
-        "test/compose-all/" + compose_all_key,
+        "test/import-links/" + import_links_key,
         "test/target/" + target_key,
     ]
 
diff --git a/tests/frontend/artifact_list_contents/elements/import-bin.bst 
b/tests/frontend/artifact_list_contents/elements/import-bin.bst
new file mode 100644
index 000000000..a847c0c23
--- /dev/null
+++ b/tests/frontend/artifact_list_contents/elements/import-bin.bst
@@ -0,0 +1,4 @@
+kind: import
+sources:
+- kind: local
+  path: files/bin-files
diff --git a/tests/frontend/artifact_list_contents/elements/import-links.bst 
b/tests/frontend/artifact_list_contents/elements/import-links.bst
new file mode 100644
index 000000000..42b279ee2
--- /dev/null
+++ b/tests/frontend/artifact_list_contents/elements/import-links.bst
@@ -0,0 +1,4 @@
+kind: import
+sources:
+- kind: local
+  path: files/files-and-links
diff --git a/tests/frontend/artifact_list_contents/elements/target.bst 
b/tests/frontend/artifact_list_contents/elements/target.bst
new file mode 100644
index 000000000..18562fe37
--- /dev/null
+++ b/tests/frontend/artifact_list_contents/elements/target.bst
@@ -0,0 +1,8 @@
+kind: stack
+description: |
+
+  Main stack target for the bst build test
+
+depends:
+- import-bin.bst
+- import-links.bst
diff --git 
a/tests/frontend/artifact_list_contents/files/bin-files/usr/bin/hello 
b/tests/frontend/artifact_list_contents/files/bin-files/usr/bin/hello
new file mode 100755
index 000000000..f534a4083
--- /dev/null
+++ b/tests/frontend/artifact_list_contents/files/bin-files/usr/bin/hello
@@ -0,0 +1,3 @@
+#!/bin/bash
+
+echo "Hello !"
diff --git 
a/tests/frontend/artifact_list_contents/files/files-and-links/basicfile 
b/tests/frontend/artifact_list_contents/files/files-and-links/basicfile
new file mode 100644
index 000000000..d03e2425c
--- /dev/null
+++ b/tests/frontend/artifact_list_contents/files/files-and-links/basicfile
@@ -0,0 +1 @@
+file contents
diff --git 
a/tests/frontend/artifact_list_contents/files/files-and-links/basicfolder/subdir-file
 
b/tests/frontend/artifact_list_contents/files/files-and-links/basicfolder/subdir-file
new file mode 100644
index 000000000..e69de29bb
diff --git a/tests/frontend/artifact_list_contents/project.conf 
b/tests/frontend/artifact_list_contents/project.conf
new file mode 100644
index 000000000..7e690f56f
--- /dev/null
+++ b/tests/frontend/artifact_list_contents/project.conf
@@ -0,0 +1,10 @@
+# Project config for frontend build test
+name: test
+min-version: 2.0
+element-path: elements
+
+plugins:
+- origin: pip
+  package-name: sample-plugins
+  sources:
+  - git

Reply via email to