Skip to content
This repository has been archived by the owner on Sep 14, 2024. It is now read-only.

Commit

Permalink
Introduce new test scripts (#59)
Browse files Browse the repository at this point in the history
* Introduce new test scripts

* Update references to test scripts

* Use success/fail/total counts

* Upgrade Lemur
  • Loading branch information
LPGhatguy authored Mar 24, 2020
1 parent 26692e6 commit dc2f2fa
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 106 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
- name: Test
run: |
lua -lluacov spec.lua
lua -lluacov test/lemur.lua
luacheck src tests
# luacov-coveralls default settings do not function on GitHub Actions.
Expand Down
12 changes: 10 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# Test places and build artifacts
/*.rbxlx
/*.rbxl
/*.rbxmx
/*.rbxm

# LuaCov reports
/luacov.*

# MkDocs build output
/site
/lua_install

# Cargo build output
/target
/target
16 changes: 13 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,31 @@ To get started working on TestEZ, you'll need:
* [Luacheck](https://github.com/mpeterv/luacheck) (`luarocks install luacheck`)
* [LuaCov](https://keplerproject.github.io/luacov) (`luarocks install luacov`)

Once you have all of these installed, you can run `lua bin/install-dependencies.lua` script to grab a couple additional local dependencies automatically.
Make sure to clone the repository with submodules. You can do that to your existing repository with:

```sh
git submodule update --init
```

Finally, you can run all of TestEZ's tests with:

```sh
lua spec.lua
lua test/lemur.lua
```

Or, to generate a LuaCov coverage report:

```sh
lua -lluacov spec.lua
lua -lluacov test/lemur.lua
luacov
```

If you're an engineer at Roblox, you can skip this setup and use Roblox-CLI. Make sure it's on your `PATH` and that you have a production Roblox Studio installation. You can then run:

```sh
./test/roblox-cli.sh
```

## Pull Requests
Before starting a pull request, open an issue about the feature or bug. This helps us prevent duplicated and wasted effort. These issues are a great place to ask for help if you run into problems!

Expand Down
8 changes: 4 additions & 4 deletions default.project.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "TestEZ",
"tree": {
"$path": "src"
}
"name": "TestEZ",
"tree": {
"$path": "src"
}
}
96 changes: 0 additions & 96 deletions spec.lua

This file was deleted.

40 changes: 40 additions & 0 deletions test-place.project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "TestEZ Test Place",
"tree": {
"$className": "DataModel",

"ReplicatedStorage": {
"$className": "ReplicatedStorage",

"TestEZ": {
"$path": "src"
},

"TestEZTests": {
"$path": "tests"
}
},

"ServerScriptService": {
"$className": "ServerScriptService",

"Run Tests": {
"$path": "test/runner.server.lua"
}
},

"HttpService": {
"$className": "HttpService",
"$properties": {
"HttpEnabled": true
}
},

"Players": {
"$className": "Players",
"$properties": {
"CharacterAutoLoads": false
}
}
}
}
32 changes: 32 additions & 0 deletions test/lemur.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--[[
Loads TestEZ and all of its dependencies, then runs our test entrypoint.
]]

-- If you add any dependencies, add them to this table so they'll be loaded!
local LOAD_MODULES = {
{"src", "TestEZ"},
{"tests", "TestEZTests"},
}

-- This makes sure we can load Lemur and other libraries that depend on init.lua
package.path = package.path .. ";?/init.lua"

-- If this fails, make sure you've cloned all Git submodules of this repo!
local lemur = require("modules.lemur")

-- Create a virtual Roblox tree
local habitat = lemur.Habitat.new()

-- We'll put all of our library code and dependencies here
local ReplicatedStorage = habitat.game:GetService("ReplicatedStorage")

-- Load all of the modules specified above
for _, module in ipairs(LOAD_MODULES) do
local container = habitat:loadFromFs(module[1])
container.Name = module[2]
container.Parent = ReplicatedStorage
end

-- When Lemur implements a proper scheduling interface, we'll use that instead.
local runTests = habitat:loadFromFs("test/runner.server.lua")
habitat:require(runTests)
18 changes: 18 additions & 0 deletions test/roblox-cli.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/sh

# Usage: ./test/roblox-cli.sh

if [ ! -z ${LOCALAPPDATA+x} ]; then
# Probably Windows, look for any Roblox installation in the default path.

VERSIONS_FOLDER="$LOCALAPPDATA/Roblox/Versions"
INSTALL=`find "$VERSIONS_FOLDER" -maxdepth 1 -name version-* | head -1`
CONTENT="$INSTALL/content"
else
# Probably macOS, look for Roblox Studio in its default path.

CONTENT="/Applications/RobloxStudio.App/Contents/Resources/content"
fi

rojo build test-place.project.json -o TestPlace.rbxlx
roblox-cli run --load.place TestPlace.rbxlx --assetFolder "$CONTENT"
98 changes: 98 additions & 0 deletions test/runner.server.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
--[[
This test runner is invoked in all the environments that we want to test our
library in.
We target Lemur, Roblox Studio, and Roblox-CLI.
]]

-- luacheck: globals __LEMUR__

local isRobloxCli, ProcessService = pcall(game.GetService, game, "ProcessService")

local function findUnitTests(container, foundTests)
foundTests = foundTests or {}

for _, child in ipairs(container:GetChildren()) do
if child:IsA("ModuleScript") then
table.insert(foundTests, child)
end

findUnitTests(child, foundTests)
end

return foundTests
end

local completed, result = xpcall(function()
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local testModules = findUnitTests(ReplicatedStorage.TestEZTests)

local totalCount = 0
local failureCount = 0
local successCount = 0
local errorMessages = {}

for _, testModule in ipairs(testModules) do
local tests = require(testModule)

print(string.format("%s", testModule.Name))

for testName, testFunction in pairs(tests) do
local success, message = pcall(testFunction)
totalCount = totalCount + 1

if success then
print(string.format(" [PASS] %s", testName))
successCount = successCount + 1
else
print(string.format(" [FAIL] %s", testName))
failureCount = failureCount + 1

local logMessage = string.format("Test: %s\nError: %s", testName, message)
table.insert(errorMessages, logMessage)
end
end
end

print()
print(string.format("%s tests run: %s passed, %s failed", totalCount, successCount, failureCount))

if #errorMessages > 0 then
print()
print(table.concat(errorMessages, "\n\n"))
end

return failureCount == 0 and 0 or 1
end, debug.traceback)

local statusCode
local errorMessage = nil
if completed then
statusCode = result
else
statusCode = 1
errorMessage = result
end

if __LEMUR__ then
-- Lemur has access to normal Lua OS APIs

if errorMessage ~= nil then
print(errorMessage)
end
os.exit(statusCode)
elseif isRobloxCli then
-- Roblox CLI has a special service to terminate the process

if errorMessage ~= nil then
print(errorMessage)
end
ProcessService:Exit(statusCode)
else
-- In Studio, we can just throw an error to get the user's attention

if errorMessage ~= nil then
error(errorMessage, 0)
end
end

0 comments on commit dc2f2fa

Please sign in to comment.