Skip to content

Commit

Permalink
Use lune for unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dionysusnu committed Sep 17, 2024
1 parent 9adc14e commit 3d63096
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 42 deletions.
17 changes: 2 additions & 15 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ jobs:
- name: Compile
run: npm run build
unit-tests:
if: false
needs: [build]
name: Unit Tests
runs-on: windows-latest
Expand All @@ -41,25 +40,13 @@ jobs:
- name: Checkout repository
uses: actions/[email protected]

- name: Install Roblox Studio
uses: OrbitalOwen/[email protected]
with:
cookie: ${{ secrets.ROBLOSECURITY || '_|WARNING:-DO-NOT-SHARE-THIS.--Sharing-this-will-allow-someone-to-log-in-as-you-and-to-steal-your-ROBUX-and-items.|_C4D95B1043562D1055F60AAD48C2EF833CED19A4C04639B9558E9D1CCF2AEA1718B65A404D1C553877E9E74F30A3FA55CABE3211B9496B9BBC425C8979334A9FF3C4EDE5EC33DA6A4A7CC0CDEF2FCA493B88D458EEEFC962FB1F729D01C830D98A3D0338400A0D843CDF8EFE6B42406208CA259398C6C207559D56F0429CD9D567FE6E6CCFCE3C4C87CFAC12DF8F5A0EAC7DB5E6F28D2DA7F722123DBBBA26C8339684F947C915758CC619EC51006A615C82AA087D63772D0CC59473AA35802558434770C7A0163B41E9C7E2937F51B0F8A4C0C493411B7CF470604D39B2ADEC460EDD1AB35086FC3B764770B43A8B7E6D6997AB32A0990E548E118090980192BF581C68754D39D2AC11AE2A2D7FDFC7C4CB34902CBD0B19816DC4BB023F0538C69487EAC48BEC3A7DCED3D142BE185BD103F336C24CD89EA2C3F4D3BBF8F672C922997B1CB503A2860C8F659C1661B517D1C9D7' }}
token: ${{ secrets.GITHUB_TOKEN }}

- name: Install Foreman
- name: Install Foreman dependencies
uses: rojo-rbx/[email protected]
with:
token: ${{ secrets.GITHUB_TOKEN }}

- name: Install NPM dependencies
uses: bahmutov/[email protected]

- name: Compile and run tests
- name: Run tests
run: npm test

# - name: Report Coverage
# continue-on-error: true
# uses: coverallsapp/[email protected]
# with:
# github-token: ${{ secrets.GITHUB_TOKEN }}
12 changes: 3 additions & 9 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ name: Publish

on:
workflow_dispatch:
# schedule:
# - cron: "0 7 * * *"
schedule:
- cron: "0 7 * * *"

jobs:
publish:
Expand All @@ -17,13 +17,7 @@ jobs:
- name: Checkout repository
uses: actions/[email protected]

- name: Install Roblox Studio
uses: OrbitalOwen/[email protected]
with:
cookie: ${{ secrets.ROBLOSECURITY || '_|WARNING:-DO-NOT-SHARE-THIS.--Sharing-this-will-allow-someone-to-log-in-as-you-and-to-steal-your-ROBUX-and-items.|_C4D95B1043562D1055F60AAD48C2EF833CED19A4C04639B9558E9D1CCF2AEA1718B65A404D1C553877E9E74F30A3FA55CABE3211B9496B9BBC425C8979334A9FF3C4EDE5EC33DA6A4A7CC0CDEF2FCA493B88D458EEEFC962FB1F729D01C830D98A3D0338400A0D843CDF8EFE6B42406208CA259398C6C207559D56F0429CD9D567FE6E6CCFCE3C4C87CFAC12DF8F5A0EAC7DB5E6F28D2DA7F722123DBBBA26C8339684F947C915758CC619EC51006A615C82AA087D63772D0CC59473AA35802558434770C7A0163B41E9C7E2937F51B0F8A4C0C493411B7CF470604D39B2ADEC460EDD1AB35086FC3B764770B43A8B7E6D6997AB32A0990E548E118090980192BF581C68754D39D2AC11AE2A2D7FDFC7C4CB34902CBD0B19816DC4BB023F0538C69487EAC48BEC3A7DCED3D142BE185BD103F336C24CD89EA2C3F4D3BBF8F672C922997B1CB503A2860C8F659C1661B517D1C9D7' }}
token: ${{ secrets.GITHUB_TOKEN }}

- name: Install Foreman
- name: Install Foreman dependencies
uses: rojo-rbx/[email protected]
with:
token: ${{ secrets.GITHUB_TOKEN }}
Expand Down
1 change: 1 addition & 0 deletions foreman.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[tools]
rojo = {source = "rojo-rbx/rojo", version = "6.0.0-rc.1"}
run-in-roblox = {source = "rojo-rbx/run-in-roblox", version = "0.3.0"}
lune = { source = "filiptibell/lune", version = "0.8.8" }
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
"prepublishOnly": "npm run eslint && npm test",
"build": "rbxtsc --verbose",
"eslint": "eslint src tests --max-warnings 0",
"test": "npm run test-types && npm run build && npm run test-link && npm run test-compile && npm run test-rojo && npm run test-run",
"test-types": "cd tests && npm i @rbxts/types@latest @rbxts/compiler-types@latest",
"test": "npm run build && npm run test-types && npm run test-link && npm run test-compile && npm run test-rojo && npm run test-run",
"test-lune": "npm run build && npm run test-types && npm run test-link && npm run test-compile && npm run test-rojo && npm run test-run-lune",
"test-types": "cd tests && npm i @rbxts/types@latest",
"test-link": "npm link && cd tests && npm link @rbxts/rust-classes",
"test-compile": "cd tests && rbxtsc --verbose",
"test-rojo": "rojo build tests -o ./tests/test.rbxlx",
"test-run": "run-in-roblox --place ./tests/test.rbxlx --script ./tests/out/main.server.lua",
"test-run-lune": "lune run ./tests/runTestsWithLune.lua ./tests/test.rbxlx",
"dev-add-version": "copy package.json package-temp.json & for /F \"tokens=*\" %n IN ('git rev-parse --short HEAD') DO (cat package.json | jq --tab \".version |= . + \\\"-dev-%n\\\"\" > package-new.json & move package-new.json package.json)",
"dev-undo-version": "move package-temp.json package.json",
"dev-publish": "npm run dev-add-version && npm publish --tag next & npm run dev-undo-version"
Expand Down
30 changes: 16 additions & 14 deletions tests/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
"author": "",
"license": "ISC",
"dependencies": {
"@rbxts/compiler-types": "^2.2.0-types.0",
"@rbxts/compiler-types": "^3.0.0-types.0",
"@rbxts/services": "^1.1.2",
"@rbxts/testez": "^0.3.1-ts.6",
"@rbxts/types": "^1.0.744"
"@rbxts/types": "^1.0.807",
"roblox-ts": "^3.0.0"
}
}
70 changes: 70 additions & 0 deletions tests/runTestsWithLune.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
-- Taken and modified from roblox-ts under the MIT license
-- https://github.com/roblox-ts/roblox-ts/blob/835d1959860b45a7cdf520f7d4dd2b16a1477c94/tests/runTestsWithLune.lua

local roblox = require("@lune/roblox")
local fs = require("@lune/fs")
local luau = require("@lune/luau")
local process = require("@lune/process")

local testPlacePath = process.args[1]

local game = roblox.deserializePlace(fs.readFile(testPlacePath))

local function tableJoin(...)
local result = {}
for i = 1, select("#", ...) do
for k, v in select(i, ...) do
result[k] = v
end
end
return result
end

-- not 100% accurate to tick() functionality, but good enough for TestEZ usage
local function tick()
return os.clock()
end

-- RuntimeLib uses :WaitForChild(), but tests don't need networking so :FindFirstChild() should be fine
roblox.implementMethod("Instance", "WaitForChild", function(self, ...)
return self:FindFirstChild(...)
end)

-- Promise.lua indexes RunService.Heartbeat, but only uses it in Promise.defer and Promise.delay
roblox.implementProperty("RunService", "Heartbeat", function()
return {}
end, function() end)

local robloxRequire

local function runRobloxScript(script: LuaSourceContainer)
local callableFn = luau.load(luau.compile(script.Source), {
debugName = script:GetFullName(),
environment = tableJoin(roblox, {
game = game,
script = script,
require = robloxRequire,
tick = tick,
}),
})

return callableFn()
end

local requireCache = {}

function robloxRequire(moduleScript: ModuleScript)
-- the same script instance sometimes gives a different ref
-- unsure why, but using :GetFullName() fixes this for now
local scriptPath = moduleScript:GetFullName()
local cached = requireCache[scriptPath]
if cached then
return table.unpack(cached)
end

local result = table.pack(runRobloxScript(moduleScript))
requireCache[scriptPath] = result
return table.unpack(result)
end

runRobloxScript(game.ServerScriptService.main)

0 comments on commit 3d63096

Please sign in to comment.