You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@buildstream.apache.org by tv...@apache.org on 2022/04/05 10:04:41 UTC

[buildstream-plugins] 26/49: tests/sources/patch.py: Adding tests for patch source

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

tvb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/buildstream-plugins.git

commit d35af6e0df21a2bc09fb4b6877996f19304d903e
Author: Tristan van Berkom <tr...@codethink.co.uk>
AuthorDate: Mon Mar 21 14:39:44 2022 +0900

    tests/sources/patch.py: Adding tests for patch source
---
 tests/sources/patch.py                             | 197 +++++++++++++++++++++
 tests/sources/patch/basic/failure-empty-dir.bst    |   5 +
 .../patch/basic/failure-nonexistent-dir.bst        |   6 +
 tests/sources/patch/basic/file.txt                 |   1 +
 tests/sources/patch/basic/file_1.patch             |   7 +
 tests/sources/patch/basic/irregular.bst            |   7 +
 tests/sources/patch/basic/project.conf             |   3 +
 tests/sources/patch/basic/target.bst               |   7 +
 tests/sources/patch/different-strip-level/file.txt |   1 +
 .../patch/different-strip-level/file_1.patch       |   7 +
 .../patch/different-strip-level/project.conf       |   3 +
 .../sources/patch/different-strip-level/target.bst |   8 +
 .../patch/invalid-relative-path/file_1.patch       |   7 +
 .../patch/invalid-relative-path/irregular.bst      |   5 +
 .../patch/invalid-relative-path/project.conf       |   3 +
 tests/sources/patch/multiple-patches/file.txt      |   1 +
 tests/sources/patch/multiple-patches/file_1.patch  |   7 +
 tests/sources/patch/multiple-patches/file_2.patch  |   7 +
 tests/sources/patch/multiple-patches/project.conf  |   3 +
 tests/sources/patch/multiple-patches/target.bst    |   9 +
 .../sources/patch/separate-patch-dir/file_1.patch  |   7 +
 .../separate-patch-dir/files/test-dir/file.txt     |   1 +
 .../sources/patch/separate-patch-dir/project.conf  |   3 +
 tests/sources/patch/separate-patch-dir/target.bst  |   8 +
 24 files changed, 313 insertions(+)

diff --git a/tests/sources/patch.py b/tests/sources/patch.py
new file mode 100644
index 0000000..91848f1
--- /dev/null
+++ b/tests/sources/patch.py
@@ -0,0 +1,197 @@
+# Pylint doesn't play well with fixtures and dependency injection from pytest
+# pylint: disable=redefined-outer-name
+
+import os
+import socket
+import pytest
+
+from buildstream.exceptions import ErrorDomain, LoadErrorReason
+from buildstream._testing import cli  # pylint: disable=unused-import
+
+DATA_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "patch",)
+
+
+# generate_file_types()
+#
+# Generator that creates a regular file directory, symbolic link, fifo
+# and socket at the specified path.
+#
+# Args:
+#  path: (str) path where to create each different type of file
+#
+def generate_file_types(path):
+    def clean():
+        if os.path.exists(path):
+            if os.path.isdir(path):
+                os.rmdir(path)
+            else:
+                os.remove(path)
+
+    clean()
+
+    with open(path, "w", encoding="utf-8"):
+        pass
+    yield
+    clean()
+
+    os.makedirs(path)
+    yield
+    clean()
+
+    os.symlink("project.conf", path)
+    yield
+    clean()
+
+    os.mkfifo(path)
+    yield
+    clean()
+
+    # Change directory because the full path may be longer than the ~100
+    # characters permitted for a unix socket
+    old_dir = os.getcwd()
+    parent, child = os.path.split(path)
+    os.chdir(parent)
+
+    s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+
+    try:
+        s.bind(child)
+        os.chdir(old_dir)
+        yield
+    finally:
+        s.close()
+
+    clean()
+
+
+@pytest.mark.datafiles(os.path.join(DATA_DIR, "basic"))
+def test_missing_patch(cli, datafiles):
+    project = str(datafiles)
+
+    # Removing the local file causes preflight to fail
+    localfile = os.path.join(project, "file_1.patch")
+    os.remove(localfile)
+
+    result = cli.run(project=project, args=["show", "target.bst"])
+    result.assert_main_error(ErrorDomain.LOAD, LoadErrorReason.MISSING_FILE)
+
+
+@pytest.mark.datafiles(os.path.join(DATA_DIR, "basic"))
+def test_non_regular_file_patch(cli, datafiles):
+    project = str(datafiles)
+
+    patch_path = os.path.join(project, "irregular_file.patch")
+    for _file_type in generate_file_types(patch_path):
+        result = cli.run(project=project, args=["show", "irregular.bst"])
+        if os.path.isfile(patch_path) and not os.path.islink(patch_path):
+            result.assert_success()
+        else:
+            result.assert_main_error(ErrorDomain.LOAD, LoadErrorReason.PROJ_PATH_INVALID_KIND)
+
+
+@pytest.mark.datafiles(os.path.join(DATA_DIR, "basic"))
+def test_invalid_absolute_path(cli, datafiles):
+    project = str(datafiles)
+
+    with open(os.path.join(project, "target.bst"), "r", encoding="utf-8") as f:
+        old_yaml = f.read()
+    new_yaml = old_yaml.replace("file_1.patch", os.path.join(project, "file_1.patch"))
+    assert old_yaml != new_yaml
+
+    with open(os.path.join(project, "target.bst"), "w", encoding="utf-8") as f:
+        f.write(new_yaml)
+
+    result = cli.run(project=project, args=["show", "target.bst"])
+    result.assert_main_error(ErrorDomain.LOAD, LoadErrorReason.PROJ_PATH_INVALID)
+
+
+@pytest.mark.datafiles(os.path.join(DATA_DIR, "invalid-relative-path"))
+def test_invalid_relative_path(cli, datafiles):
+    project = str(datafiles)
+
+    result = cli.run(project=project, args=["show", "irregular.bst"])
+    result.assert_main_error(ErrorDomain.LOAD, LoadErrorReason.PROJ_PATH_INVALID)
+
+
+@pytest.mark.datafiles(os.path.join(DATA_DIR, "basic"))
+def test_stage_and_patch(cli, tmpdir, datafiles):
+    project = str(datafiles)
+    checkoutdir = os.path.join(str(tmpdir), "checkout")
+
+    # Build, checkout
+    result = cli.run(project=project, args=["build", "target.bst"])
+    result.assert_success()
+    result = cli.run(project=project, args=["artifact", "checkout", "target.bst", "--directory", checkoutdir])
+    result.assert_success()
+
+    # Test the file.txt was patched and changed
+    with open(os.path.join(checkoutdir, "file.txt"), encoding="utf-8") as f:
+        assert f.read() == "This is text file with superpowers\n"
+
+
+@pytest.mark.datafiles(os.path.join(DATA_DIR, "basic"))
+def test_stage_file_nonexistent_dir(cli, datafiles):
+    project = str(datafiles)
+
+    # Fails at build time because it tries to patch into a non-existing directory
+    result = cli.run(project=project, args=["build", "failure-nonexistent-dir.bst"])
+    result.assert_main_error(ErrorDomain.STREAM, None)
+    result.assert_task_error(ErrorDomain.SOURCE, "patch-no-files")
+
+
+@pytest.mark.datafiles(os.path.join(DATA_DIR, "basic"))
+def test_stage_file_empty_dir(cli, datafiles):
+    project = str(datafiles)
+
+    # Fails at build time because it tries to patch with nothing else staged
+    result = cli.run(project=project, args=["build", "failure-empty-dir.bst"])
+    result.assert_main_error(ErrorDomain.STREAM, None)
+    result.assert_task_error(ErrorDomain.SOURCE, "patch-no-files")
+
+
+@pytest.mark.datafiles(os.path.join(DATA_DIR, "separate-patch-dir"))
+def test_stage_separate_patch_dir(cli, tmpdir, datafiles):
+    project = str(datafiles)
+    checkoutdir = os.path.join(str(tmpdir), "checkout")
+
+    # Track, fetch, build, checkout
+    result = cli.run(project=project, args=["build", "target.bst"])
+    result.assert_success()
+    result = cli.run(project=project, args=["artifact", "checkout", "target.bst", "--directory", checkoutdir])
+    result.assert_success()
+
+    # Test the file.txt was patched and changed
+    with open(os.path.join(checkoutdir, "test-dir", "file.txt"), encoding="utf-8") as f:
+        assert f.read() == "This is text file in a directory with superpowers\n"
+
+
+@pytest.mark.datafiles(os.path.join(DATA_DIR, "multiple-patches"))
+def test_stage_multiple_patches(cli, tmpdir, datafiles):
+    project = str(datafiles)
+    checkoutdir = os.path.join(str(tmpdir), "checkout")
+
+    # Track, fetch, build, checkout
+    result = cli.run(project=project, args=["build", "target.bst"])
+    result.assert_success()
+    result = cli.run(project=project, args=["artifact", "checkout", "target.bst", "--directory", checkoutdir])
+    result.assert_success()
+
+    # Test the file.txt was patched and changed
+    with open(os.path.join(checkoutdir, "file.txt"), encoding="utf-8") as f:
+        assert f.read() == "This is text file with more superpowers\n"
+
+
+@pytest.mark.datafiles(os.path.join(DATA_DIR, "different-strip-level"))
+def test_patch_strip_level(cli, tmpdir, datafiles):
+    project = str(datafiles)
+    checkoutdir = os.path.join(str(tmpdir), "checkout")
+
+    # Track, fetch, build, checkout
+    result = cli.run(project=project, args=["build", "target.bst"])
+    result.assert_success()
+    result = cli.run(project=project, args=["artifact", "checkout", "target.bst", "--directory", checkoutdir])
+    result.assert_success()
+
+    # Test the file.txt was patched and changed
+    with open(os.path.join(checkoutdir, "file.txt"), encoding="utf-8") as f:
+        assert f.read() == "This is text file with superpowers\n"
diff --git a/tests/sources/patch/basic/failure-empty-dir.bst b/tests/sources/patch/basic/failure-empty-dir.bst
new file mode 100644
index 0000000..b22af27
--- /dev/null
+++ b/tests/sources/patch/basic/failure-empty-dir.bst
@@ -0,0 +1,5 @@
+kind: import
+description: This is also the pony
+sources:
+- kind: patch
+  path: file_1.patch
diff --git a/tests/sources/patch/basic/failure-nonexistent-dir.bst b/tests/sources/patch/basic/failure-nonexistent-dir.bst
new file mode 100644
index 0000000..8fd593d
--- /dev/null
+++ b/tests/sources/patch/basic/failure-nonexistent-dir.bst
@@ -0,0 +1,6 @@
+kind: import
+description: This is also the pony
+sources:
+- kind: patch
+  path: file_1.patch
+  directory: /idontexist
diff --git a/tests/sources/patch/basic/file.txt b/tests/sources/patch/basic/file.txt
new file mode 100644
index 0000000..a496efe
--- /dev/null
+++ b/tests/sources/patch/basic/file.txt
@@ -0,0 +1 @@
+This is a text file
diff --git a/tests/sources/patch/basic/file_1.patch b/tests/sources/patch/basic/file_1.patch
new file mode 100644
index 0000000..424a486
--- /dev/null
+++ b/tests/sources/patch/basic/file_1.patch
@@ -0,0 +1,7 @@
+diff --git a/file.txt b/file.txt
+index a496efe..341ef26 100644
+--- a/file.txt
++++ b/file.txt
+@@ -1 +1 @@
+-This is a text file
++This is text file with superpowers
diff --git a/tests/sources/patch/basic/irregular.bst b/tests/sources/patch/basic/irregular.bst
new file mode 100644
index 0000000..425cbcc
--- /dev/null
+++ b/tests/sources/patch/basic/irregular.bst
@@ -0,0 +1,7 @@
+kind: import
+description: This is the pony
+sources:
+- kind: local
+  path: file.txt
+- kind: patch
+  path: irregular_file.patch
diff --git a/tests/sources/patch/basic/project.conf b/tests/sources/patch/basic/project.conf
new file mode 100644
index 0000000..dc34380
--- /dev/null
+++ b/tests/sources/patch/basic/project.conf
@@ -0,0 +1,3 @@
+# Basic project
+name: foo
+min-version: 2.0
diff --git a/tests/sources/patch/basic/target.bst b/tests/sources/patch/basic/target.bst
new file mode 100644
index 0000000..913371d
--- /dev/null
+++ b/tests/sources/patch/basic/target.bst
@@ -0,0 +1,7 @@
+kind: import
+description: This is the pony
+sources:
+- kind: local
+  path: file.txt
+- kind: patch
+  path: file_1.patch
diff --git a/tests/sources/patch/different-strip-level/file.txt b/tests/sources/patch/different-strip-level/file.txt
new file mode 100644
index 0000000..a496efe
--- /dev/null
+++ b/tests/sources/patch/different-strip-level/file.txt
@@ -0,0 +1 @@
+This is a text file
diff --git a/tests/sources/patch/different-strip-level/file_1.patch b/tests/sources/patch/different-strip-level/file_1.patch
new file mode 100644
index 0000000..ff7f7fe
--- /dev/null
+++ b/tests/sources/patch/different-strip-level/file_1.patch
@@ -0,0 +1,7 @@
+diff --git foo/a/file.txt foo/b/file.txt
+index a496efe..341ef26 100644
+--- foo/a/file.txt
++++ foo/b/file.txt
+@@ -1 +1 @@
+-This is a text file
++This is text file with superpowers
diff --git a/tests/sources/patch/different-strip-level/project.conf b/tests/sources/patch/different-strip-level/project.conf
new file mode 100644
index 0000000..dc34380
--- /dev/null
+++ b/tests/sources/patch/different-strip-level/project.conf
@@ -0,0 +1,3 @@
+# Basic project
+name: foo
+min-version: 2.0
diff --git a/tests/sources/patch/different-strip-level/target.bst b/tests/sources/patch/different-strip-level/target.bst
new file mode 100644
index 0000000..c8ea19a
--- /dev/null
+++ b/tests/sources/patch/different-strip-level/target.bst
@@ -0,0 +1,8 @@
+kind: import
+description: This is the pony
+sources:
+- kind: local
+  path: file.txt
+- kind: patch
+  path: file_1.patch
+  strip-level: 2
diff --git a/tests/sources/patch/invalid-relative-path/file_1.patch b/tests/sources/patch/invalid-relative-path/file_1.patch
new file mode 100644
index 0000000..424a486
--- /dev/null
+++ b/tests/sources/patch/invalid-relative-path/file_1.patch
@@ -0,0 +1,7 @@
+diff --git a/file.txt b/file.txt
+index a496efe..341ef26 100644
+--- a/file.txt
++++ b/file.txt
+@@ -1 +1 @@
+-This is a text file
++This is text file with superpowers
diff --git a/tests/sources/patch/invalid-relative-path/irregular.bst b/tests/sources/patch/invalid-relative-path/irregular.bst
new file mode 100644
index 0000000..6b63a4e
--- /dev/null
+++ b/tests/sources/patch/invalid-relative-path/irregular.bst
@@ -0,0 +1,5 @@
+kind: import
+description: This is the pony
+sources:
+- kind: patch
+  path: ../invalid-relative-path/irregular_file.patch
diff --git a/tests/sources/patch/invalid-relative-path/project.conf b/tests/sources/patch/invalid-relative-path/project.conf
new file mode 100644
index 0000000..dc34380
--- /dev/null
+++ b/tests/sources/patch/invalid-relative-path/project.conf
@@ -0,0 +1,3 @@
+# Basic project
+name: foo
+min-version: 2.0
diff --git a/tests/sources/patch/multiple-patches/file.txt b/tests/sources/patch/multiple-patches/file.txt
new file mode 100644
index 0000000..a496efe
--- /dev/null
+++ b/tests/sources/patch/multiple-patches/file.txt
@@ -0,0 +1 @@
+This is a text file
diff --git a/tests/sources/patch/multiple-patches/file_1.patch b/tests/sources/patch/multiple-patches/file_1.patch
new file mode 100644
index 0000000..424a486
--- /dev/null
+++ b/tests/sources/patch/multiple-patches/file_1.patch
@@ -0,0 +1,7 @@
+diff --git a/file.txt b/file.txt
+index a496efe..341ef26 100644
+--- a/file.txt
++++ b/file.txt
+@@ -1 +1 @@
+-This is a text file
++This is text file with superpowers
diff --git a/tests/sources/patch/multiple-patches/file_2.patch b/tests/sources/patch/multiple-patches/file_2.patch
new file mode 100644
index 0000000..f56614b
--- /dev/null
+++ b/tests/sources/patch/multiple-patches/file_2.patch
@@ -0,0 +1,7 @@
+diff --git a/file.txt b/file.txt
+index a496efe..341ef26 100644
+--- a/file.txt
++++ b/file.txt
+@@ -1 +1 @@
+-This is text file with superpowers
++This is text file with more superpowers
diff --git a/tests/sources/patch/multiple-patches/project.conf b/tests/sources/patch/multiple-patches/project.conf
new file mode 100644
index 0000000..dc34380
--- /dev/null
+++ b/tests/sources/patch/multiple-patches/project.conf
@@ -0,0 +1,3 @@
+# Basic project
+name: foo
+min-version: 2.0
diff --git a/tests/sources/patch/multiple-patches/target.bst b/tests/sources/patch/multiple-patches/target.bst
new file mode 100644
index 0000000..4665e7d
--- /dev/null
+++ b/tests/sources/patch/multiple-patches/target.bst
@@ -0,0 +1,9 @@
+kind: import
+description: This is the pony
+sources:
+- kind: local
+  path: file.txt
+- kind: patch
+  path: file_1.patch
+- kind: patch
+  path: file_2.patch
diff --git a/tests/sources/patch/separate-patch-dir/file_1.patch b/tests/sources/patch/separate-patch-dir/file_1.patch
new file mode 100644
index 0000000..ae8bc33
--- /dev/null
+++ b/tests/sources/patch/separate-patch-dir/file_1.patch
@@ -0,0 +1,7 @@
+diff --git a/file.txt b/file.txt
+index a496efe..341ef26 100644
+--- a/file.txt
++++ b/file.txt
+@@ -1 +1 @@
+-This is a text file in a directory
++This is text file in a directory with superpowers
diff --git a/tests/sources/patch/separate-patch-dir/files/test-dir/file.txt b/tests/sources/patch/separate-patch-dir/files/test-dir/file.txt
new file mode 100644
index 0000000..425911a
--- /dev/null
+++ b/tests/sources/patch/separate-patch-dir/files/test-dir/file.txt
@@ -0,0 +1 @@
+This is a text file in a directory
diff --git a/tests/sources/patch/separate-patch-dir/project.conf b/tests/sources/patch/separate-patch-dir/project.conf
new file mode 100644
index 0000000..dc34380
--- /dev/null
+++ b/tests/sources/patch/separate-patch-dir/project.conf
@@ -0,0 +1,3 @@
+# Basic project
+name: foo
+min-version: 2.0
diff --git a/tests/sources/patch/separate-patch-dir/target.bst b/tests/sources/patch/separate-patch-dir/target.bst
new file mode 100644
index 0000000..796c131
--- /dev/null
+++ b/tests/sources/patch/separate-patch-dir/target.bst
@@ -0,0 +1,8 @@
+kind: import
+description: This is the pony
+sources:
+- kind: local
+  path: files
+- kind: patch
+  path: file_1.patch
+  directory: test-dir