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

Unity Testsuite #176

Closed
wants to merge 14 commits into from
Closed
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
10 changes: 10 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,13 @@

## Requires SpacetimeDB PRs
*List any PRs here that are required for this SDK change to work*

## Testsuite
*If you would like to run against a specific SpacetimeDB branch in the testsuite, specify that here. This can be a branch name or a link to a PR.*

SpacetimeDB branch name: master
Copy link
Collaborator Author

@jdetter jdetter Oct 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's call this a ref instead


## Testing
*Write instructions for a test that you performed for this PR*

- [ ] Describe a test for this PR that you have completed
119 changes: 119 additions & 0 deletions .github/workflows/unity-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
name: Unity Test Suite

on:
push:
branches:
- staging
pull_request:

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3

# Grab the branch name from the PR description. If it's not found, master will be used instead.
- name: Extract SpacetimeDB branch name or PR link from PR description
id: extract-branch
if: github.event_name == 'pull_request'
run: |
description=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
${{ github.event.pull_request.url }} | jq -r '.body')

# Check if description contains a branch name or a PR link
branch_or_pr=$(echo "$description" | grep -oP '(?<=SpacetimeDB branch name:\s).+')
echo "Branch or PR found: $branch_or_pr"

if [[ -z "$branch_or_pr" ]]; then
branch="master"
elif [[ "$branch_or_pr" =~ ^https://github.com/.*/pull/[0-9]+$ ]]; then
# If it's a PR link, extract the branch name from the PR
pr_number=$(echo "$branch_or_pr" | grep -oP '[0-9]+$')
branch=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
https://api.github.com/repos/clockworklabs/SpacetimeDB/pulls/$pr_number | jq -r '.head.ref')
else
# It's already a branch name
branch="$branch_or_pr"
fi

echo "branch=$branch" >> $GITHUB_OUTPUT
echo "Final branch name: $branch"

- name: Replace com.clockworklabs.spacetimedbsdk in manifest.json
run: |
# Get the branch name from the environment variable
branch_name="${{ github.head_ref }}"
# Replace any reference to com.clockworklabs.spacetimedbsdk with the correct GitHub URL using the current branch
sed -i "s|\"com.clockworklabs.spacetimedbsdk\":.*|\"com.clockworklabs.spacetimedbsdk\": \"https://github.com/clockworklabs/com.clockworklabs.spacetimedbsdk.git#$branch_name\",|" unity-tests~/client/Packages/manifest.json

cat unity-tests~/client/Packages/manifest.json
- name: Install Rust toolchain
run: |
curl https://sh.rustup.rs -sSf | sh -s -- -y
source $HOME/.cargo/env
rustup install stable
rustup default stable

- name: Install SpacetimeDB CLI from specific branch
run: |
cd unity-tests~
git clone https://github.com/clockworklabs/SpacetimeDB.git
cd SpacetimeDB
# Sanitize the branch name by trimming any newlines or spaces
branch_name=$(echo "${{ steps.extract-branch.outputs.branch }}" | tr -d '[:space:]')
# If the branch name is not found, default to master
if [ -z "$branch_name" ]; then
branch_name="master"
fi
git checkout "$branch_name"
echo "Checked out branch: $branch_name"
cargo build --release -p spacetimedb-cli
sudo mv target/release/spacetime /usr/bin/spacetime

- name: Generate client bindings
run: |
cd unity-tests~/server
bash ./generate.sh -y

- name: Start SpacetimeDB
run: |
spacetime start &
disown

- uses: actions/cache@v3
with:
path: |
unity-tests~/server/target
~/.cargo
key: server-target-SpacetimeDBUnityTestsuite-Linux-x86-${{ runner.os }}-${{ hashFiles('unity-tests~/server/Cargo.lock') }}
restore-keys: |
server-target-SpacetimeDBUnityTestsuite-Linux-x86-${{ runner.os }}-
server-target-SpacetimeDBUnityTestsuite-

- name: Publish module to SpacetimeDB
run: |
cd unity-tests~/server
bash ./publish.sh

- uses: actions/cache@v3
with:
path: unity-tests~/client/Library
key: Library-SpacetimeDBUnityTestsuite-Linux-x86
restore-keys: |
Library-SpacetimeDBUnityTestsuite-
Library-

- name: Set up Unity
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uses: game-ci/unity-test-runner@v4
with:
unityVersion: 2022.3.32f1 # Adjust Unity version to a valid tag
projectPath: unity-tests~/client # Path to the Unity project subdirectory
githubToken: ${{ secrets.GITHUB_TOKEN }}
testMode: playmode
useHostNetwork: true
env:
UNITY_EMAIL: ${{ secrets.UNITY_EMAIL }}
UNITY_PASSWORD: ${{ secrets.UNITY_PASSWORD }}
UNITY_SERIAL: ${{ secrets.UNITY_SERIAL }}

79 changes: 79 additions & 0 deletions .github/workflows/unity-testsuite-bindings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
name: Check Unity Testsuite Bindings

on:
push:
branches:
- staging
pull_request:

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3

# Grab the branch name from the PR description. If it's not found, master will be used instead.
- name: Extract SpacetimeDB branch name or PR link from PR description
id: extract-branch
if: github.event_name == 'pull_request'
run: |
description=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
${{ github.event.pull_request.url }} | jq -r '.body')

# Check if description contains a branch name or a PR link
branch_or_pr=$(echo "$description" | grep -oP '(?<=SpacetimeDB branch name:\s).+')
echo "Branch or PR found: $branch_or_pr"

if [[ -z "$branch_or_pr" ]]; then
branch="master"
elif [[ "$branch_or_pr" =~ ^https://github.com/.*/pull/[0-9]+$ ]]; then
# If it's a PR link, extract the branch name from the PR
pr_number=$(echo "$branch_or_pr" | grep -oP '[0-9]+$')
branch=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
https://api.github.com/repos/clockworklabs/SpacetimeDB/pulls/$pr_number | jq -r '.head.ref')
else
# It's already a branch name
branch="$branch_or_pr"
fi

echo "branch=$branch" >> $GITHUB_OUTPUT
echo "Final branch name: $branch"
- name: Install Rust toolchain
run: |
curl https://sh.rustup.rs -sSf | sh -s -- -y
source $HOME/.cargo/env
rustup install stable
rustup default stable

- name: Install SpacetimeDB CLI from specific branch
run: |
cd unity-tests~
git clone https://github.com/clockworklabs/SpacetimeDB.git
cd SpacetimeDB
# Sanitize the branch name by trimming any newlines or spaces
branch_name=$(echo "${{ steps.extract-branch.outputs.branch }}" | tr -d '[:space:]')
# If the branch name is not found, default to master
if [ -z "$branch_name" ]; then
branch_name="master"
fi
git checkout "$branch_name"
echo "Checked out branch: $branch_name"
cargo build --release -p spacetimedb-cli
sudo mv target/release/spacetime /usr/bin/spacetime

- name: Generate client bindings
run: |
cd unity-tests~/server
bash ./generate.sh -y

- name: Check for changes
run: |
git diff --exit-code
continue-on-error: true

- name: Fail if there are changes
if: ${{ steps.check-for-changes.outcome == 'failure' }}
run: |
echo "Error: Bindings are dirty. Please generate bindings again and commit them to this branch."
exit 1
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,4 @@ obj~
# This is used for local paths to SpacetimeDB packages.
/nuget.config
/nuget.config.meta
.idea/
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
unity-tests~/
14 changes: 13 additions & 1 deletion SpacetimeDB.ClientSDK.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
Expand All @@ -19,19 +19,31 @@
<AssemblyVersion>1.0.0</AssemblyVersion>
<Version>1.0.0-rc1</Version>
<DefaultItemExcludes>$(DefaultItemExcludes);*~/**</DefaultItemExcludes>
<!-- Disable default compile items to prevent duplicate inclusion -->
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
<!-- We want to save DLLs for Unity which doesn't support NuGet. -->
<RestorePackagesPath>packages</RestorePackagesPath>
</PropertyGroup>

<!-- Package references -->
<ItemGroup>
<PackageReference Include="SpacetimeDB.BSATN.Runtime" Version="1.0.0-rc1" />

<InternalsVisibleTo Include="SpacetimeDB.Tests" />
</ItemGroup>

<!-- Include non-code files -->
<ItemGroup>
<None Update="logo.png" Pack="true" PackagePath="" />
<None Update="README.md" Pack="true" PackagePath="" />
</ItemGroup>

<!-- Explicitly include all .cs files except Unity-related scripts -->
<ItemGroup>
<Compile Include="src/**/*.cs" Exclude="unity-tests/client/Assets/**/*.cs" />

<!-- Optionally include Unity-related scripts for Unity builds -->
<Compile Include="unity-tests/client/Assets/**/*.cs" Condition="'$(BuildingUnity)' == 'true'" />
</ItemGroup>

</Project>
8 changes: 8 additions & 0 deletions packages/SpacetimeDB.BSATN.Codegen.0.9.2.meta

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

8 changes: 8 additions & 0 deletions packages/SpacetimeDB.BSATN.Codegen.0.9.2/netstandard2.0.meta

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

8 changes: 8 additions & 0 deletions packages/spacetimedb.bsatn.runtime/0.12.0.meta

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

40 changes: 40 additions & 0 deletions unity-tests~/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
.idea/
.vsconfig

### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

### macOS Patch ###
# iCloud generated files
*.icloud

# End of https://www.toptal.com/developers/gitignore/api/macos

# SpacetimeDB repo used for the testsuite
SpacetimeDB/
35 changes: 35 additions & 0 deletions unity-tests~/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
## Unity Testsuite

This testsuite is designed to test the SDK against a real Unity project. This project was initially branched off of the [SpacetimeDBCircleGame](https://github.com/clockworklabs/SpacetimeDBCircleGame)

### Running Locally

1. clone this repository
2. Open UnityHub
3. Make sure you have a valid Unity license
4. Add -> `com.clockworklabs.spacetimedbsdk/unity-tests/client`
5. Open the project called `client` at the top of your project list

Now, while that's loading you can get SpacetimeDB setup. The easiest thing to do will be this:
```
# cd into your unity-tests directory
cd com.clockworklabs.spacetimedbsdk/unity-tests
# clone a new instance of SpacetimeDB within this directory
git clone ssh://[email protected]/clockworklabs/SpacetimeDB
cd SpacetimeDB
git checkout a-branch-I-want-to-test-against
# You probably want to install this version of the CLI for generation + publishing
cargo install --path ./crates/cli
# start spacetimedb, Boppy recommends starting this in a separate terminal window
spacetime start &

# generate bindings
bash server/generate.sh

# publish module
bash server/publish.sh
```

After you've done this you can open `Window -> General -> Test Runner` which will open the test runner interface. If you look in `Play Mode Tests` you should see some tests you can run. You can run tests individually, run selected tests or run all tests.

*NOTE: The Unity project that you open is called "client" and it has a local reference to the unity package so if you make changes to the unity package code it will update automatically in the editor.*
Loading
Loading