Skip to content

Commit 34d44cf

Browse files
committed
Add a CI test to generate builds from the JSON api data and check
differences
1 parent ff2fadc commit 34d44cf

File tree

14 files changed

+4015
-21
lines changed

14 files changed

+4015
-21
lines changed

.busted

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,11 @@ return {
99
helper = "HeadlessWrapper.lua",
1010
ROOT = { "../spec" },
1111
["exclude-tags"] = "builds",
12+
},
13+
generate = {
14+
directory = "src",
15+
lpath = "../runtime/lua/?.lua;../runtime/lua/?/init.lua",
16+
helper = "HeadlessWrapper.lua",
17+
ROOT = { "../spec/GenerateBuilds.lua" },
1218
}
1319
}

.github/workflows/test.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,16 @@ jobs:
3232
- run: git config --global --add safe.directory $(pwd)
3333
- name: Check if the generated ModCache is different
3434
run: git diff --exit-code src/Data/ModCache.lua
35+
check_generated_builds:
36+
runs-on: ubuntu-latest
37+
container: ghcr.io/pathofbuildingcommunity/pathofbuilding-tests:latest
38+
steps:
39+
- name: Install xmllint and git dependency
40+
run: apk add libxml2-utils git
41+
- name: Checkout
42+
uses: actions/checkout@v4
43+
- name: Generate Builds
44+
run: busted --lua=luajit -r generate
45+
- run: git config --global --add safe.directory $(pwd)
46+
- name: Check if the generated builds are different
47+
run: git diff --exit-code spec

docker-compose.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,14 @@ services:
1313
working_dir: /workdir
1414
volumes:
1515
- ./:/workdir:ro
16+
busted-generate:
17+
image: ghcr.io/pathofbuildingcommunity/pathofbuilding-tests:latest
18+
environment:
19+
HOME: /tmp
20+
container_name: pathofbuilding-generate
21+
command: sh -c "apk add libxml2-utils && busted --lua=luajit -r generate"
22+
security_opt:
23+
- no-new-privileges:true
24+
working_dir: /workdir
25+
volumes:
26+
- ./:/workdir:rw

spec/GenerateBuilds.lua

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
local dkjson = require "dkjson"
2+
3+
local function fetchBuilds(path, buildList)
4+
buildList = buildList or {}
5+
for file in lfs.dir(path) do
6+
if file ~= "." and file ~= ".." then
7+
local f = path .. '/' .. file
8+
local attr = lfs.attributes(f)
9+
assert(type(attr) == "table")
10+
if attr.mode == "directory" then
11+
fetchBuilds(f, buildList)
12+
else
13+
if file:match("^.+(%..+)$") == ".json" then
14+
local fileHnd, errMsg = io.open(f, "r")
15+
if not fileHnd then
16+
return nil, errMsg
17+
end
18+
local fileText = fileHnd:read("*a")
19+
fileHnd:close()
20+
local fullCharData = dkjson.decode(fileText)
21+
buildList[f] = fullCharData.character
22+
end
23+
end
24+
end
25+
end
26+
return buildList
27+
end
28+
29+
function buildTable(tableName, values, string, indent)
30+
string = string or ""
31+
indent = indent or 0
32+
local indentStr = string.rep("\t", indent)
33+
local nextIndentStr = string.rep("\t", indent + 1)
34+
string = string .. indentStr .. tableName .. " = {\n"
35+
for key, value in pairsSortByKey(values) do
36+
if type(value) == "table" then
37+
string = buildTable("[\"" .. key .. "\"]", value, string, indent + 1) .. ",\n"
38+
elseif type(value) == "boolean" then
39+
string = string .. nextIndentStr .. "[\"" .. key .. "\"] = " .. (value and "true" or "false") .. ",\n"
40+
elseif type(value) == "string" then
41+
string = string .. nextIndentStr .. "[\"" .. key .. "\"] = \"" .. value .. "\",\n"
42+
else
43+
string = string .. nextIndentStr .. "[\"" .. key .. "\"] = " .. round(value, 4) .. ",\n"
44+
end
45+
end
46+
string = string .. indentStr .. "}"
47+
return string
48+
end
49+
50+
local function formatXmlFile(filepath)
51+
local command = "xmllint --c14n " .. filepath
52+
53+
-- Open the command process for reading ('r')
54+
local handle = io.popen(command, 'r')
55+
if not handle then
56+
return nil, "Failed to run xmllint. Is it installed and in your PATH?"
57+
end
58+
59+
-- Read the entire output from the command
60+
local result = handle:read("*a")
61+
handle:close()
62+
63+
local fileHnd, errMsg = io.open(filepath:gsub("-unformatted", ""), "w")
64+
fileHnd:write(result)
65+
fileHnd:close()
66+
end
67+
68+
expose("generate all builds", function()
69+
local buildList = fetchBuilds("../spec/TestBuilds")
70+
for filename, testBuild in pairs(buildList) do
71+
local buildName = filename:gsub("^.*/([^/]+/[^/]+)%.[^.]+$", "%1")
72+
73+
it("on build: " .. buildName, function()
74+
-- Load the build and calculate stats
75+
print("Loading build: " .. buildName)
76+
loadBuildFromJSON(testBuild, testBuild)
77+
78+
print("Saving main output")
79+
-- Save the calculated output to a Lua file in the generated sub-folder
80+
-- Get rid of circular reference issues first
81+
build.calcsTab.mainOutput.ReqDexItem = nil
82+
build.calcsTab.mainOutput.ReqIntItem = nil
83+
build.calcsTab.mainOutput.ReqStrItem = nil
84+
local outputFile = filename:gsub("^(.+)%..+$", "%1.lua")
85+
outputFile = outputFile:gsub("^(.+)/([^/]+)$", "%1/generated/%2")
86+
local fileHnd, errMsg = io.open(outputFile, "w+")
87+
fileHnd:write(buildTable("output", build.calcsTab.mainOutput))
88+
fileHnd:close()
89+
90+
-- Save the XML DB file in the generated sub-folder
91+
print("Saving xml db file")
92+
build.dbFileName = outputFile:gsub("^(.+)%..+$", "%1-unformatted.xml")
93+
build:SaveDBFile()
94+
-- Format/order the XML file to easily see differences with previous generations
95+
formatXmlFile(build.dbFileName)
96+
-- Remove the unformatted XML file
97+
os.remove(build.dbFileName)
98+
end)
99+
end
100+
end)

0 commit comments

Comments
 (0)