From 5603e5d33b1375fd3743ea7dff7d7e0b881b4061 Mon Sep 17 00:00:00 2001
From: LoveIsGrief <loveisgrief@tuta.io>
Date: Sun, 20 Aug 2023 15:22:40 +0200
Subject: [PATCH] test: Add tests for using build.context

These are simple tests to make sure that the generated docker-compose.json
looks like it should. This means has a build.context and does NOT
have an image defined - the image is built when starting the service.
---
 src/haskell/test/Arion/NixSpec.hs             | 16 ++++++++
 src/haskell/test/Spec.hs                      |  2 +
 .../Arion/NixSpec/arion-context-compose.json  | 41 +++++++++++++++++++
 .../Arion/NixSpec/arion-context-compose.nix   |  9 ++++
 .../Arion/NixSpec/build-context/Dockerfile    |  4 ++
 5 files changed, 72 insertions(+)
 create mode 100644 src/haskell/testdata/Arion/NixSpec/arion-context-compose.json
 create mode 100644 src/haskell/testdata/Arion/NixSpec/arion-context-compose.nix
 create mode 100644 src/haskell/testdata/Arion/NixSpec/build-context/Dockerfile

diff --git a/src/haskell/test/Arion/NixSpec.hs b/src/haskell/test/Arion/NixSpec.hs
index e20bebf..18fc5a8 100644
--- a/src/haskell/test/Arion/NixSpec.hs
+++ b/src/haskell/test/Arion/NixSpec.hs
@@ -1,6 +1,7 @@
 {-# LANGUAGE OverloadedStrings #-}
 module Arion.NixSpec
   ( spec
+    , contextSpec
   )
 where
 
@@ -27,6 +28,21 @@ spec = describe "evaluateComposition" $ it "matches an example" $ do
   expected <- T.readFile "src/haskell/testdata/Arion/NixSpec/arion-compose.json"
   censorPaths actual `shouldBe` censorPaths expected
 
+contextSpec :: Spec
+contextSpec = describe "evaluateComposition" $ it "matches an build.context example" $ do
+  x <- Arion.Nix.evaluateComposition EvaluationArgs
+    { evalUid      = 1234
+    , evalModules  = NEL.fromList
+                       ["src/haskell/testdata/Arion/NixSpec/arion-context-compose.nix"]
+    , evalPkgs     = "import <nixpkgs> { system = \"x86_64-linux\"; }"
+    , evalWorkDir  = Nothing
+    , evalMode     = ReadOnly
+    , evalUserArgs = ["--show-trace"]
+    }
+  let actual = pretty x
+  expected <- T.readFile "src/haskell/testdata/Arion/NixSpec/arion-context-compose.json"
+  censorPaths actual `shouldBe` censorPaths expected
+
 censorPaths :: Text -> Text
 censorPaths = censorImages . censorStorePaths
 
diff --git a/src/haskell/test/Spec.hs b/src/haskell/test/Spec.hs
index d2da234..3bcd7df 100644
--- a/src/haskell/test/Spec.hs
+++ b/src/haskell/test/Spec.hs
@@ -9,3 +9,5 @@ import qualified Arion.NixSpec
 spec :: Spec
 spec = do
   describe "Arion.Nix" Arion.NixSpec.spec
+  describe "Arion.Nix" Arion.NixSpec.contextSpec
+
diff --git a/src/haskell/testdata/Arion/NixSpec/arion-context-compose.json b/src/haskell/testdata/Arion/NixSpec/arion-context-compose.json
new file mode 100644
index 0000000..ae67f53
--- /dev/null
+++ b/src/haskell/testdata/Arion/NixSpec/arion-context-compose.json
@@ -0,0 +1,41 @@
+{
+    "networks": {
+        "default": {
+            "name": "unit-test-data"
+        }
+    },
+    "services": {
+        "webserver": {
+            "build": {
+                "context": "<STOREPATH>"
+            },
+            "environment": {},
+            "ports": [
+                "8080:80"
+            ],
+            "sysctls": {},
+            "volumes": []
+        }
+    },
+    "version": "3.4",
+    "volumes": {},
+    "x-arion": {
+        "images": [
+            {
+                "imageExe": "<STOREPATH>",
+                "imageName": "localhost/webserver",
+                "imageTag": "<HASH>"
+            }
+        ],
+        "project": {
+            "name": "unit-test-data"
+        },
+        "serviceInfo": {
+            "webserver": {
+                "defaultExec": [
+                    "/bin/sh"
+                ]
+            }
+        }
+    }
+}
diff --git a/src/haskell/testdata/Arion/NixSpec/arion-context-compose.nix b/src/haskell/testdata/Arion/NixSpec/arion-context-compose.nix
new file mode 100644
index 0000000..a4c5b7c
--- /dev/null
+++ b/src/haskell/testdata/Arion/NixSpec/arion-context-compose.nix
@@ -0,0 +1,9 @@
+{
+  project.name = "unit-test-data";
+  services.webserver.service = {
+    build.context = "${./build-context}";
+    ports = [
+      "8080:80"
+    ];
+  };
+}
diff --git a/src/haskell/testdata/Arion/NixSpec/build-context/Dockerfile b/src/haskell/testdata/Arion/NixSpec/build-context/Dockerfile
new file mode 100644
index 0000000..d2ba487
--- /dev/null
+++ b/src/haskell/testdata/Arion/NixSpec/build-context/Dockerfile
@@ -0,0 +1,4 @@
+FROM nginx
+
+RUN echo this is a dockerfile to be built
+