Skip to content

Commit 1ac1f94

Browse files
committed
improved testing and fixed bugs
1 parent 0722e4d commit 1ac1f94

File tree

14 files changed

+920
-69
lines changed

14 files changed

+920
-69
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ jobs:
3232
- name: Build test binary
3333
run: cargo test --no-run --verbose
3434
- name: Run tests
35-
run: |
36-
cargo test --verbose --no-fail-fast
37-
npm --prefix ./bindings/tmc-langs-node run jest
35+
run: cargo test --verbose --no-fail-fast
3836

3937
windows:
4038
runs-on: windows-latest
@@ -48,9 +46,7 @@ jobs:
4846
- name: Build test binary
4947
run: cargo test --no-run --verbose
5048
- name: Run tests
51-
run: |
52-
cargo test --verbose --no-fail-fast
53-
cd ./bindings/tmc-langs-node && npm run jest
49+
run: cargo test --verbose --no-fail-fast
5450

5551
macos:
5652
runs-on: macos-latest
@@ -62,6 +58,4 @@ jobs:
6258
- name: Build test binary
6359
run: cargo test --no-run --verbose
6460
- name: Run tests
65-
run: |
66-
cargo test --verbose --no-fail-fast
67-
npm --prefix ./bindings/tmc-langs-node run jest
61+
run: cargo test --verbose --no-fail-fast

Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
[workspace]
22
members =[
3+
# core libraries
34
"tmc-client",
45
"tmc-langs",
56
"tmc-langs-cli",
67
"tmc-langs-framework",
78
"tmc-langs-plugins",
89
"tmc-langs-util",
910

11+
# language plugins
1012
"plugins/csharp",
1113
"plugins/java",
1214
"plugins/make",
1315
"plugins/notests",
1416
"plugins/python3",
1517
"plugins/r",
1618

19+
# bindings to other languages
1720
"bindings/tmc-langs-node",
21+
22+
# test helpers
23+
"tmc-server-mock",
1824
]
1925
resolver = "2"

bindings/tmc-langs-node/Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ crate-type = ["cdylib"]
1212
[dependencies.neon]
1313
version = "0.8"
1414
default-features = false
15-
features = ["napi-6"]
15+
features = ["napi-6", "default-panic-hook"]
1616

1717
[dependencies]
1818
tmc-langs = { path = "../../tmc-langs", features = ["ts"] }
@@ -23,3 +23,9 @@ serde = "1"
2323
serde_json = "1"
2424
thiserror = "1"
2525
ts-rs = { git = "https://github.com/Heliozoa/ts-rs/", branch = "ext" }
26+
27+
[dev-dependencies]
28+
log = "0.4"
29+
once_cell = "1"
30+
simple_logger = "1"
31+
tmc-server-mock = { path = "../../tmc-server-mock" }

bindings/tmc-langs-node/jest/tmc.test.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path from "path";
33
import fs, { existsSync } from "fs";
44
import functions from "../ts/functions";
55
import { Tmc } from "../ts/tmc";
6+
import { env } from "process";
67

78
var initiated = false;
89
function init(): Tmc {
@@ -11,7 +12,13 @@ function init(): Tmc {
1112
functions.initLogging();
1213
}
1314

14-
const tmc = new Tmc("mock-client", "mock-version", `http://localhost:3000`);
15+
var addr;
16+
if (env.TMC_LANGS_MOCK_SERVER_ADDR) {
17+
addr = env.TMC_LANGS_MOCK_SERVER_ADDR;
18+
} else {
19+
addr = `http://localhost:3000`;
20+
}
21+
const tmc = new Tmc("mock-client", "mock-version", addr);
1522
tmc.loginWithToken("");
1623
return tmc;
1724
}
@@ -23,10 +30,17 @@ async function initWithTempDir(): Promise<Tmc> {
2330
}
2431

2532
const tempDir = await tempdir();
33+
34+
var addr;
35+
if (env.TMC_LANGS_MOCK_SERVER_ADDR) {
36+
addr = env.TMC_LANGS_MOCK_SERVER_ADDR;
37+
} else {
38+
addr = `http://localhost:3000`;
39+
}
2640
const tmc = new Tmc(
2741
"mock-client",
2842
"mock-version",
29-
`http://localhost:3000`,
43+
addr,
3044
path.join(tempDir, "config"),
3145
path.join(tempDir, "projects")
3246
);
@@ -300,8 +314,10 @@ test("gets exercise submissions", async () => {
300314
test("gets exercise updates", async () => {
301315
const tmc = init();
302316

303-
const exerciseUpdates = tmc.getExerciseUpdates(1, new Map([[1, ""]]));
317+
const map: [number, string][] = [[1, "old checksum"], [9999, "old checksum"]];
318+
const exerciseUpdates = tmc.getExerciseUpdates(1, map);
304319
expect(exerciseUpdates.created.length).toBeGreaterThan(0);
320+
expect(exerciseUpdates.updated.length).toBeGreaterThan(0);
305321
});
306322

307323
test("gets organizations", async () => {

bindings/tmc-langs-node/src/helpers.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ macro_rules! lock {
1717
macro_rules! parse_arg {
1818
($cx: ident, $ty: path, $i: expr) => {{
1919
let arg = $cx.argument::<JsValue>($i)?;
20-
crate::de::from_value::<_, $ty>(&mut $cx, arg).unwrap()
20+
crate::de::from_value::<_, $ty>(&mut $cx, arg).expect("failed to parse argument")
2121
}};
2222
}
2323

@@ -95,8 +95,8 @@ pub fn convert_err<E: Error>(cx: &mut FunctionContext, e: E) -> Throw {
9595
trace.push(s.to_string());
9696
source = s.source();
9797
}
98-
let err = crate::ser::to_value(cx, &trace).unwrap();
99-
cx.throw::<_, ()>(err).unwrap_err()
98+
let err = crate::ser::to_value(cx, &trace).expect("failed to convert error");
99+
cx.throw::<_, ()>(err).expect_err("should never happen")
100100
}
101101

102102
pub fn convert_res<'a, T: Serialize, E: Error>(

bindings/tmc-langs-node/src/lib.rs

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ mod ser;
66
use crate::helpers::{convert, convert_err, convert_res};
77
use neon::prelude::*;
88
use std::{
9-
collections::HashMap,
109
env,
1110
error::Error,
1211
io::{Cursor, Read},
@@ -172,7 +171,7 @@ fn prepare_submission(mut cx: FunctionContext) -> JsResult<JsValue> {
172171
output_path: PathBuf,
173172
stub_zip_path: Option<PathBuf>,
174173
submission_path: PathBuf,
175-
tmc_param: HashMap<String, Vec<String>>,
174+
tmc_param: Vec<(String, Vec<String>)>,
176175
top_level_dir_name: Option<String>
177176
);
178177

@@ -441,11 +440,12 @@ fn get_exercise_updates(mut cx: FunctionContext) -> JsResult<JsValue> {
441440
client_name: String,
442441
client_version: String,
443442
course_id: u32,
444-
exercise: HashMap<u32, String>
443+
exercise: Vec<(u32, String)>
445444
);
446445

446+
let map = exercise.into_iter().collect();
447447
let res = with_client(client_name, client_version, |client| {
448-
Ok(client.get_exercise_updates(course_id, exercise)?)
448+
Ok(client.get_exercise_updates(course_id, map)?)
449449
});
450450
convert_res(&mut cx, res)
451451
}
@@ -915,3 +915,42 @@ ts_rs::export! {
915915
tmc_langs::TmcConfig
916916
=> "ts/generated.d.ts",
917917
}
918+
919+
#[cfg(test)]
920+
mod test {
921+
use std::{env, process::Command};
922+
923+
use once_cell::sync::OnceCell;
924+
use tmc_server_mock::mockito::{server_address, Mock};
925+
926+
static MOCKS: OnceCell<Vec<Mock>> = OnceCell::new();
927+
928+
fn init() {
929+
use log::*;
930+
use simple_logger::*;
931+
let _ = SimpleLogger::new()
932+
.with_level(LevelFilter::Debug)
933+
.with_module_level("mockito", LevelFilter::Info)
934+
.init();
935+
936+
let _ = MOCKS.get_or_init(tmc_server_mock::mock_all);
937+
}
938+
939+
#[test]
940+
fn jest() {
941+
init();
942+
env::set_var(
943+
"TMC_LANGS_MOCK_SERVER_ADDR",
944+
format!("http://{}", server_address()),
945+
);
946+
let s = Command::new("npm")
947+
.args(&["run", "jest"])
948+
.output()
949+
.expect("running jest failed");
950+
println!("stdout: {}", String::from_utf8_lossy(&s.stdout));
951+
println!("stderr: {}", String::from_utf8_lossy(&s.stderr));
952+
if !s.status.success() {
953+
panic!("jest test failed")
954+
}
955+
}
956+
}

bindings/tmc-langs-node/ts/functions.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function prepareSubmission(
3636
outputPath: string,
3737
stubZipPath: string | null,
3838
submissionPath: string,
39-
tmcParam: Map<string, Array<string>>,
39+
tmcParam: Array<[string, Array<string>]>,
4040
topLevelDirName: string | null
4141
): void;
4242
export function refreshCourse(
@@ -111,7 +111,7 @@ export function getExerciseUpdates(
111111
clientName: string,
112112
clientVersion: string,
113113
courseId: number,
114-
exercise: Map<number, string>
114+
exercise: Array<[number, string]>
115115
): types.UpdateResult;
116116
export function getOrganization(
117117
clientName: string,

bindings/tmc-langs-node/ts/tmc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export class Tmc {
7878
outputPath: string,
7979
stubZipPath: string | null,
8080
submissionPath: string,
81-
tmcParam: Map<string, Array<string>>,
81+
tmcParam: Array<[string, Array<string>]>,
8282
topLevelDirName: string | null
8383
): void {
8484
return tmc.prepareSubmission(
@@ -199,7 +199,7 @@ export class Tmc {
199199

200200
getExerciseUpdates(
201201
courseId: number,
202-
exercise: Map<number, string>
202+
exercise: Array<[number, string]>
203203
): types.UpdateResult {
204204
return tmc.getExerciseUpdates(
205205
this.clientName,

run-tests.sh

Lines changed: 0 additions & 7 deletions
This file was deleted.

tmc-langs-framework/src/command.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ mod test {
218218

219219
#[test]
220220
fn timeout() {
221-
let cmd = TmcCommand::piped("sleep").with(|e| e.arg("1"));
221+
let cmd = TmcCommand::piped("sleep").with(|e| e.arg("2"));
222222
assert!(matches!(
223223
cmd.output_with_timeout(Duration::from_nanos(1)),
224224
Err(TmcError::Command(CommandError::TimeOut { .. }))

0 commit comments

Comments
 (0)