Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Garry's Mod compatibility #9

Merged
merged 4 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ root = true
charset = utf-8
end_of_line = LF
quote_type = single
insert_final_newline = true
trim_trailing_whitespace = true

[*.lua]
indent_style = space
Expand Down
22 changes: 20 additions & 2 deletions .github/workflows/publish_version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on:
push:
tags:
- 'v*'

jobs:
publish_rock_and_build_ubuntu:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -67,6 +67,23 @@ jobs:
name: jestronaut-windows
path: dist/jestronaut-windows.exe

build_gmod_addon:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18'
- name: Install dependencies
run: npm ci
- name: Build the GMod addon
run: npm run build:gmod
- name: Upload GMod addon artifact
uses: actions/upload-artifact@v3
with:
name: jestronaut-gmod
path: dist/gmod-addon.zip

publish_github:
runs-on: ubuntu-latest
needs: [publish_rock_and_build_ubuntu, build_windows]
Expand Down Expand Up @@ -95,4 +112,5 @@ jobs:
title: "Latest (automatic build)"
files: |
dist/jestronaut-ubuntu
dist/jestronaut-windows.exe
dist/jestronaut-windows.exe
dist/gmod-addon.zip
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ node_modules
lua_modules

/luacov.stats.out
/luacov.report.out
/luacov.report.out

# Do not include the symlinked/copied lib files in the addon
/gmod-addon/lua/tests
/gmod-addon/lua/jestronaut/
/gmod-addon/lua/jestronaut.lua
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ This binary is a standalone executable that can be used to run Jestronaut tests
>
> The variable `MSYS_NO_PATHCONV=1` is only required on Windows when using MSYS2 (for example with Git for Windows). It prevents the path from being converted to a Windows path, which would result in an incorrect path.

### Running tests in Garry's Mod

See [the Jestronaut Garry's Mod addon README](./gmod-addon/README.md) for more information.

## 🧪 API

Jestronaut tries to match [the API of Jest](https://jestjs.io/docs/api) as closely as possible. Here are some examples of how to use it:
Expand Down Expand Up @@ -238,4 +242,4 @@ Use `luarocks test` in the root of this repo to execute the Jestronaut tests.

For coverage install the following LuaRocks modules:
* `luarocks install luacov`
* `luarocks install luacov-reporter-lcov`
* `luarocks install luacov-reporter-lcov`
82 changes: 82 additions & 0 deletions gmod-addon/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Jestronaut - Garry's Mod Addon`

## Installation

1. Download the addon from the latest Jestronaut release from the
[releases page](https://github.com/luttje/jestronaut/releases).

2. Extract the contents of the `gmod-addon.zip` file to your Garry's Mod `addons` folder.

3. Restart Garry's Mod if it was running.

## Usage

1. Create some test files in your project, for example:

```lua
-- lua/tests/print_test.lua

describe("print", function()
it("should print 'Hello, World!'", function()
local mockFn = jestronaut:fn()

expect(mockFn)['not']:toHaveBeenCalled()
mockFn("Hello, World!")

expect(mockFn):toHaveBeenCalledWith("Hello, World!")
end)
end)
```

2. Create a runner script in your project, for example:

```lua
-- lua/run-all-tests.lua

include("gmod-jestronaut.lua"):withGlobals()

jestronaut
:configure({
roots = {
-- Where your tests are located, needed for the reporter to find the source files:
"lua/tests/",
},

-- Prints to console:
reporter = GmodReporter
})
:registerTests(function()
include("tests/print_test.lua")
end)
:runTests()
```

3. Run the runner script in Garry's Mod:

```
lua_openscript run-all-tests.lua
Running script run-all-tests.lua...
🚀 Starting 2 tests at 17:26:02...

STARTED: o root

STARTED: o print

STARTED: o should print 'Hello, World!'

FINISHED: ✓ should print 'Hello, World!'

FINISHED: ✓ print

FINISHED: ✓ root


🎉 All tests passed. Great job!

Tests: 2 passed, 2 total
Time: 0.0019999999999527s

Ran all test suites.
```

*Note that the emoji icons are displayed as `□□` in the Garry's Mod console due to insufficient Unicode support.*
37 changes: 37 additions & 0 deletions gmod-addon/lua/gmod-jestronaut.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
-- Gmod's require doesn't return any values, so we replace require with include while loading jestronaut
-- This is a hacky way to make jestronaut work with Gmod
function callWithRequireCompat(func)
local oldRequire = require
local alreadyRequired = {}

package.path = package.path or ""

require = function(path)
if (alreadyRequired[path] == nil) then
if (file.Exists(path .. ".lua", "LUA")) then
alreadyRequired[path] = {include(path .. ".lua")}
else
Msg("Could not find file: " .. path)
return false
end
end

return unpack(alreadyRequired[path])
end

local result = func()

require = oldRequire

return result
end

local jestronaut = callWithRequireCompat(function()
local jestronaut = include("jestronaut.lua")

GmodReporter = include("gmod-reporter.lua").GmodReporter

return jestronaut
end)

return jestronaut
Loading
Loading