Skip to content

Commit

Permalink
Writes linux tests, change how byond is detected by tests, fix block …
Browse files Browse the repository at this point in the history
…and list (#6)

* test scripts

* force-check

* fuck

* oops

* commands

* tries again

* hmmmhmm

* don't use tempfile anymore

* Revert "don't use tempfile anymore"

This reverts commit c796390.

* rewrite tests completely

* make this a flag

* do not do this, the user has to set it

* FUCK

* make here

* wrong version

* set it the fuck up

* hmmmhmmmmhmmm

* oops

* wrong target

* hmm

* try again

* ehhh

* print dd out

* force-check

* ignore that file

* rewrite this

* hmm

* it works like this

* hnggg

* try and catch it

* comment out test_block

* fuckin lists

* disable block for now

* frick

* print it out then

* mhm

* fix this test already

* minor stuff

* assumptions

* now it does stuff correctly

* better

* try again

* fix windows

* try this

* fuckin try this

* fuck

* print shit

* eee

* fuck off

* just allocate the whole thing then

* oopsie

* rearrange it a bit

* don't need to export that
  • Loading branch information
jupyterkat authored Nov 22, 2023
1 parent abafdc6 commit 403a32a
Show file tree
Hide file tree
Showing 11 changed files with 208 additions and 83 deletions.
2 changes: 0 additions & 2 deletions .cargo/config.toml

This file was deleted.

55 changes: 41 additions & 14 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ jobs:
uses: actions/checkout@v4

- name: Setup Toolchains
uses: actions-rs/toolchain@v1
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
target: ${{ matrix.target_name }}
targets: ${{ matrix.target_name }}

- name: Install g++ multilib (Ubuntu)
run: |
Expand Down Expand Up @@ -54,32 +54,59 @@ jobs:
toolchain: stable
command: fmt
args: --all -- --check
# TODO: write linux tests
run_test:
name: Run test

run_test_windows:
name: Run test (Windows)
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Toolchains
uses: actions-rs/toolchain@v1
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
target: i686-pc-windows-msvc
targets: i686-pc-windows-msvc

- name: Restore BYOND cache
uses: actions/cache@v3
with:
path: ~/BYOND
key: linux-byond

- name: Set up byond
run: bash ./test_byond.sh
key: windows-byond

- name: Run tests
uses: actions-rs/cargo@v1
run: |
bash ./tools/setup_byond_windows.sh
$Env:BYOND_LOCATION = "$HOME\BYOND\byond\bin"
cargo test --package byondapi-test --target i686-pc-windows-msvc --test test -- test_byondapi_with_dreamdaemon --exact --nocapture
run_test_linux:
name: Run test (Ubuntu)
runs-on: ubuntu-20.04
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Toolchains
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
command: test
args: test_byondapi_with_dreamdaemon --target i686-pc-windows-msvc
targets: i686-unknown-linux-gnu

- name: Install g++ multilib
run: |
sudo dpkg --add-architecture i386
sudo apt-get update
sudo apt-get install build-essential g++-multilib libc6-i386 libstdc++6:i386
- name: Restore BYOND cache
uses: actions/cache@v3
with:
path: ~/BYOND
key: linux-byond

- name: Run tests
run: |
bash ./tools/setup_byond_linux.sh
source $HOME/BYOND/byond/bin/byondsetup
cargo test --package byondapi-test --target i686-unknown-linux-gnu --test test -- test_byondapi_with_dreamdaemon --exact --nocapture
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ Cargo.lock

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

# Must be defined by users, since they may be on linux
.cargo/
2 changes: 1 addition & 1 deletion crates/byondapi-rs-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ crate-type = ["cdylib"]
byondapi = { path = "../byondapi-rs" }
byondapi-sys = { path = "../byondapi-sys" }
tempfile = "3.8.1"
test-cdylib = "1.1.0"
cargo_metadata = "0.18.1"
41 changes: 27 additions & 14 deletions crates/byondapi-rs-test/dm_project/dm_project.dme
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,17 @@
throw EXCEPTION("Object did not make it through FFI")

/test/proc/test_ref()
world.maxz = 1
world.maxx = 1
world.maxy = 1

var/turf/T = locate(1,1,1)
var/ret = call_ext("byondapi_test.dll", "byond:test_ref")(T)

world.maxz = 0
world.maxx = 0
world.maxy = 0

/test/proc/test_ptr()
var/x = "meow"
var/ptr = &x
Expand All @@ -55,6 +63,20 @@
/datum/data
var/name = "test name"

/test/proc/test_block()
world.maxz = 1
world.maxx = 2
world.maxy = 2

var/ret = call_ext("byondapi_test.dll", "byond:test_block")()

if(ret != 4)
throw EXCEPTION("Block failed [json_encode(ret)]")

world.maxz = 0
world.maxx = 0
world.maxy = 0

/test/proc/test_readwrite_var()
var/datum/data/stub = new()

Expand Down Expand Up @@ -102,20 +124,6 @@
if(ret != 5)
throw EXCEPTION("List length failed [json_encode(ret)]")

/test/proc/test_block()
world.maxz = 1
world.maxx = 2
world.maxy = 2

var/ret = call_ext("byondapi_test.dll", "byond:test_block")()

if(ret != 4)
throw EXCEPTION("Block failed [json_encode(ret)]")

world.maxz = 0
world.maxx = 0
world.maxy = 0

/test/proc/test_length_with_str()
var/str = "meowman"

Expand All @@ -136,6 +144,11 @@

if(L["parrot"] != 14)
throw EXCEPTION("list modification by key failed")

/test/proc/test_list_read()
var/list/L = list("cat" = 0, "dog" = 1, "parrot" = 5)
call_ext("byondapi_test.dll", "byond:test_list_read")(L)

// BEGIN_INTERNALS
// END_INTERNALS
// BEGIN_FILE_DIR
Expand Down
23 changes: 23 additions & 0 deletions crates/byondapi-rs-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,26 @@ pub unsafe extern "C" fn test_non_assoc_list(

ByondValue::new()
}

#[no_mangle]
pub unsafe extern "C" fn test_list_read(
argc: byondapi_sys::u4c,
argv: *mut ByondValue,
) -> ByondValue {
setup_panic_handler();
let args = parse_args(argc, argv);
let list = args.get(0).unwrap();

let map = list.get_list().unwrap();
let values = map
.into_iter()
.map(|item| item.get_string().unwrap())
.collect::<Vec<_>>();

assert_eq!(
values,
vec!["cat".to_owned(), "dog".to_owned(), "parrot".to_owned()]
);

ByondValue::new()
}
113 changes: 76 additions & 37 deletions crates/byondapi-rs-test/tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,59 +1,62 @@
use std::{
path::{Path, PathBuf},
process::Command,
process::{Command, Output},
};

use tempfile::TempDir;

#[test]
#[cfg(windows)]
fn test_byondapi_with_dreamdaemon() {
let dll = build_dylib();
compile();

let tempdir = tempfile::tempdir().expect("Failed to create temporary directory");

copy_to_tmp(&dll, &tempdir);
run_dreamdaemon(&tempdir);

let stderr = run_dreamdaemon(&tempdir);

check_output_rust(&tempdir);
check_output_dd(&tempdir);

#[cfg(unix)]
const LINE_COUNT: usize = 3;
#[cfg(windows)]
const LINE_COUNT: usize = 5;

if stderr.lines().count() > LINE_COUNT {
panic!("Stderr contains more than 3 lines, an error message might be printed!")
}
}

fn bin_path() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR"))
.join("dm_project")
.join("byond")
.join("bin")
match std::env::var("BYOND_LOCATION") {
Ok(value) => {
println!("Using byond from dir {value}");
value.into()
}
Err(_) => {
println!("Byond not found, using default location");
println!("To set a location for byond, set the BYOND_LOCATION environment variable to a path");
println!("Keep in mind that this path has to point to the /bin folder of byond");
"C:\\Program Files (x86)\\BYOND\\bin".into()
}
}
}

fn find_dm() -> Result<PathBuf, ()> {
let base_path = bin_path();

let path = if cfg!(windows) {
base_path.join("dm.exe")
} else {
base_path.join("DreamMaker")
};

if path.exists() {
Ok(path)
fn find_dm() -> PathBuf {
if cfg!(windows) {
bin_path().join("dm.exe")
} else {
Err(())
"DreamMaker".into()
}
}

fn find_dd() -> Result<PathBuf, ()> {
let base_path = bin_path();

let path = if cfg!(windows) {
base_path.join("dd.exe")
} else {
base_path.join("DreamDaemon")
};

if path.exists() {
Ok(path)
fn find_dd() -> PathBuf {
if cfg!(windows) {
bin_path().join("dd.exe")
} else {
Err(())
"DreamDaemon".into()
}
}

Expand All @@ -62,11 +65,35 @@ fn project_dir() -> PathBuf {
}

fn build_dylib() -> PathBuf {
test_cdylib::build_current_project()
let mut cmd = Command::new(option_env!("CARGO").unwrap_or("cargo"));

cmd.arg("build").arg("--message-format=json").arg("--lib");
#[cfg(windows)]
cmd.arg("--target=i686-pc-windows-msvc");
#[cfg(unix)]
cmd.arg("--target=i686-unknown-linux-gnu");
cmd.stderr(std::process::Stdio::inherit());
parse_output(cmd.output().unwrap())
}

fn parse_output(res: Output) -> PathBuf {
let mut artifact = None;
for message in cargo_metadata::Message::parse_stream(res.stdout.as_slice()) {
match message.unwrap() {
cargo_metadata::Message::CompilerMessage(m) => eprintln!("{}", m),
cargo_metadata::Message::CompilerArtifact(a) => artifact = Some(a),
_ => (),
}
}

if !res.status.success() {
panic!("Failed to build")
}
artifact.unwrap().filenames[0].clone().into()
}

fn compile() {
let dm_compiler = find_dm().expect("To run this integration test you must place a copy of BYOND binaries in dm_project/byond/bin");
let dm_compiler = find_dm();

let output = Command::new(dm_compiler)
.current_dir(project_dir())
Expand Down Expand Up @@ -98,17 +125,27 @@ fn copy_to_tmp(dll: &Path, tempdir: &TempDir) {
std::fs::copy(dll, target.join("byondapi_test.dll")).expect("Failed to copy byondapi_test.dll");
}

fn run_dreamdaemon(tempdir: &TempDir) {
let dream_daemon = find_dd().expect("To run this integration test you must place a copy of BYOND binaries in dm_project/byond/bin");
fn run_dreamdaemon(tempdir: &TempDir) -> String {
let dream_daemon = find_dd();

let _dd_output = Command::new(dream_daemon)
let dd_output = Command::new(dream_daemon)
.current_dir(tempdir.path())
.arg("dm_project.dmb")
.arg("-trusted")
.output()
.expect("DreamDaemon crashed");
let stdout = std::str::from_utf8(&dd_output.stdout).unwrap();
let stderr = std::str::from_utf8(&dd_output.stderr).unwrap();
if !stdout.is_empty() {
eprintln!("Stdout:-------------------------------------------------------------------");
eprintln!("{stdout}");
}

// println!("{:#?}", _dd_output);
if !stderr.is_empty() {
eprintln!("Stderr:-------------------------------------------------------------------");
eprintln!("{stderr}");
}
stderr.to_owned()
}

fn check_output_dd(tempdir: &TempDir) {
Expand All @@ -118,6 +155,7 @@ fn check_output_dd(tempdir: &TempDir) {

let log = std::fs::read_to_string(log).expect("Failed to read log");

eprintln!("DDlogs:-------------------------------------------------------------------");
eprintln!("{}", log);

assert!(
Expand All @@ -131,6 +169,7 @@ fn check_output_rust(tempdir: &TempDir) {

if log.exists() {
let log = std::fs::read_to_string(log).expect("Failed to read log");
eprintln!("Rustlogs:-----------------------------------------------------------------");
eprintln!("{}", log);
panic!("Rust error log was produced!");
}
Expand Down
Loading

0 comments on commit 403a32a

Please sign in to comment.