From cc5d9c3d4c4c4d096937a40d76afcbed441c553b Mon Sep 17 00:00:00 2001
From: jonny <ga86lad@mytum.de>
Date: Wed, 29 Dec 2021 01:53:48 +0100
Subject: [PATCH] added test logic

---
 .gitlab-ci.yml                     | 15 +++++++++-
 .vscode/settings.json              |  4 +++
 test/missing-file/output.json      | 16 +++++++++++
 test/no-layers/main.json           | 15 ++++++++++
 test/no-layers/output.json         | 45 ++++++++++++++++++++++++++++++
 test/run-tests.sh                  | 31 ++++++++++++++++++++
 test/test-output/missing-file.json | 16 +++++++++++
 7 files changed, 141 insertions(+), 1 deletion(-)
 create mode 100644 .vscode/settings.json
 create mode 100644 test/missing-file/output.json
 create mode 100644 test/no-layers/main.json
 create mode 100644 test/no-layers/output.json
 create mode 100644 test/run-tests.sh
 create mode 100644 test/test-output/missing-file.json

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 6ba892f..5589b49 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -2,10 +2,11 @@ image: haskell:8.10.7
 
 stages:
   - build
+  - test
   - trigger
 
 build-job:
-  only: 
+  only:
     refs:
       - main
   stage: build
@@ -18,6 +19,18 @@ build-job:
     paths:
       - dist
 
+run-test:
+  stage: test
+  dependencies:
+    - build-job
+  script:
+    - ./test/run-tests.sh
+  artifacts:
+    expire_in: 7 days
+    when: always
+    paths:
+      - 'test/test-output'
+
 trigger-mapservice-pipeline:
   only:
     refs:
diff --git a/.vscode/settings.json b/.vscode/settings.json
new file mode 100644
index 0000000..c32feb7
--- /dev/null
+++ b/.vscode/settings.json
@@ -0,0 +1,4 @@
+{
+    "haskell.formattingProvider": "none",
+    "json.format.enable": false
+}
\ No newline at end of file
diff --git a/test/missing-file/output.json b/test/missing-file/output.json
new file mode 100644
index 0000000..98b8b34
--- /dev/null
+++ b/test/missing-file/output.json
@@ -0,0 +1,16 @@
+{
+    "mapInfo": {},
+    "result": {
+        "exitGraph": "digraph G {\n\n}\n",
+        "mapLints": {},
+        "missingAssets": [],
+        "missingDeps": [
+            {
+                "depFatal": true,
+                "entrypoint": "main.json",
+                "neededBy": []
+            }
+        ]
+    },
+    "severity": "Innfo"
+}
diff --git a/test/no-layers/main.json b/test/no-layers/main.json
new file mode 100644
index 0000000..713ae65
--- /dev/null
+++ b/test/no-layers/main.json
@@ -0,0 +1,15 @@
+{
+  "tiledversion": "1.6.0",
+  "version": "1.6",
+  "width": 1,
+  "height": 1,
+  "tilewidth": 32,
+  "tileheight": 32,
+  "orientation": "orthogonal",
+  "layers": [],
+  "renderorder": "left-down",
+  "nextobjectid": 3,
+  "infinite": false,
+  "type": "map",
+  "tilesets": []
+}
diff --git a/test/no-layers/output.json b/test/no-layers/output.json
new file mode 100644
index 0000000..a0ba336
--- /dev/null
+++ b/test/no-layers/output.json
@@ -0,0 +1,45 @@
+{
+  "mapInfo": {
+      "main.json": {
+          "badges": []
+      }
+  },
+  "result": {
+      "exitGraph": "digraph G {\nn0[label=\"main.json\"];\n\n}\n",
+      "mapLints": {
+          "main.json": {
+              "general": [
+                  {
+                      "level": "Warning",
+                      "msg": "main.json should link back to the lobby"
+                  },
+                  {
+                      "level": "Suggestion",
+                      "msg": "consider adding meta information to your map using the \"mapName\", \"mapDescription\", \"mapLink\" properties."
+                  },
+                  {
+                      "level": "Suggestion",
+                      "msg": "document the map's copyright via the \"mapCopyright\" property."
+                  },
+                  {
+                      "level": "Error",
+                      "msg": "The map must contain at least one layer with the property \"exitUrl\" set."
+                  },
+                  {
+                      "level": "Error",
+                      "msg": "The map must have one layer named \"floorLayer\" of type \"objectgroup\"."
+                  },
+                  {
+                      "level": "Error",
+                      "msg": "The map must have one layer named \"start\"."
+                  }
+              ],
+              "layer": {},
+              "tileset": {}
+          }
+      },
+      "missingAssets": [],
+      "missingDeps": []
+  },
+  "severity": "Error"
+}
diff --git a/test/run-tests.sh b/test/run-tests.sh
new file mode 100644
index 0000000..fede534
--- /dev/null
+++ b/test/run-tests.sh
@@ -0,0 +1,31 @@
+#!/bin/bash
+TEST_FOLDERS=$(ls -d */)
+
+FAILED=0
+
+if ! command -v jq &> /dev/null
+then
+  apt-get update
+  apt-get install -y jq
+fi
+
+for folder in $TEST_FOLDERS
+do
+    dir=${folder%/}
+    if [ "$dir" != "test-output" ]; then
+        echo "running for $dir"
+        EXPECTED=$(jq . "./${dir}/output.json")
+        ACTUAL=$(cd .. && stack run -- walint --repository ./test/${dir} --config-file ./config.json --json --pretty | jq && cd test)
+
+
+        if [ "$EXPECTED" != "$ACTUAL" ]; then
+            FAILED=1
+            mkdir -p ./test-output
+            echo "$ACTUAL" > "./test-output/${dir}.json"
+            echo "$EXPECTED doenst match $ACTUAL"
+        fi
+    fi
+done
+
+exit $FAILED
+    
\ No newline at end of file
diff --git a/test/test-output/missing-file.json b/test/test-output/missing-file.json
new file mode 100644
index 0000000..8d34030
--- /dev/null
+++ b/test/test-output/missing-file.json
@@ -0,0 +1,16 @@
+{
+  "mapInfo": {},
+  "result": {
+    "exitGraph": "digraph G {\n\n}\n",
+    "mapLints": {},
+    "missingAssets": [],
+    "missingDeps": [
+      {
+        "depFatal": true,
+        "entrypoint": "main.json",
+        "neededBy": []
+      }
+    ]
+  },
+  "severity": "Info"
+}
-- 
GitLab