Skip to content

Commit

Permalink
Attach test location to identified tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdeviant committed Apr 21, 2024
1 parent ee29579 commit 7dea4a1
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 36 deletions.
10 changes: 8 additions & 2 deletions src/startest/internal/runner/backend/erlang.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import startest/context.{type Context}
@target(erlang)
import startest/internal/runner/core
@target(erlang)
import startest/locator
import startest/locator.{TestFunction}

@target(erlang)
pub fn run_tests(ctx: Context) -> Nil {
Expand All @@ -28,7 +28,13 @@ pub fn run_tests(ctx: Context) -> Nil {
|> binary_to_atom

get_exports(erlang_module_name)
|> list.map(pair.map_first(_, atom_to_binary))
|> list.map(fn(pair) {
let #(name, body) =
pair
|> pair.map_first(atom_to_binary)

TestFunction(module_name: test_file.module_name, name: name, body: body)
})
})
|> list.flatten
|> locator.identify_tests(ctx)
Expand Down
7 changes: 6 additions & 1 deletion src/startest/internal/runner/backend/javascript.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import startest/internal/gleam_toml
@target(javascript)
import startest/internal/runner/core
@target(javascript)
import startest/locator
import startest/locator.{TestFunction}

@target(javascript)
pub fn run_tests(ctx: Context) -> Promise(Nil) {
Expand All @@ -30,6 +30,11 @@ pub fn run_tests(ctx: Context) -> Promise(Nil) {

get_exports(js_module_path)
|> promise.map(array.to_list)
|> promise.map(list.map(_, fn(export) {
let #(name, body) = export

TestFunction(module_name: test_file.module_name, name: name, body: body)
}))
})
|> promise.await_list
|> promise.map(list.flatten)
Expand Down
27 changes: 19 additions & 8 deletions src/startest/internal/runner/core.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,29 @@ import startest/test_case.{
type Test, type TestOutcome, ExecutedTest, Failed, Passed, Skipped,
}
import startest/test_failure
import startest/test_tree.{type TestTree}
import startest/test_tree.{type TestLocation, type TestTree}

pub fn run_tests(tests: List(TestTree), ctx: Context) {
pub fn run_tests(tests: List(#(TestTree, TestLocation)), ctx: Context) {
let started_at = birl.utc_now()

let tests =
tests
|> list.flat_map(fn(tree) { test_tree.all_tests(tree) })
|> list.filter(fn(pair) {
let #(test_name, _) = pair
|> list.flat_map(fn(tree_with_location) {
let #(tree, location) = tree_with_location

test_tree.all_tests(tree)
|> list.map(fn(pair) {
let #(full_test_name, test_case) = pair

#(location.module_name, full_test_name, test_case)
})
})
|> list.filter(fn(tuple) {
let #(_module_name, full_test_name, _) = tuple

case ctx.config.test_name_pattern {
Some(test_name_pattern) ->
string.contains(does: test_name, contain: test_name_pattern)
string.contains(does: full_test_name, contain: test_name_pattern)
None -> True
}
})
Expand All @@ -40,9 +49,11 @@ pub fn run_tests(tests: List(TestTree), ctx: Context) {

let executed_tests =
tests
|> list.map(fn(pair) {
|> list.map(fn(tuple) {
let #(_module_name, full_test_name, test_case) = tuple

let test_case =
test_case.Test(pair.0, { pair.1 }.body, { pair.1 }.skipped)
test_case.Test(full_test_name, test_case.body, test_case.skipped)

ExecutedTest(test_case, run_test(test_case))
})
Expand Down
50 changes: 25 additions & 25 deletions src/startest/locator.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import simplifile
import startest/context.{type Context}
import startest/logger
import startest/test_case.{type Test, Test}
import startest/test_tree.{type TestTree, decode_test_tree}
import startest/test_tree.{
type TestLocation, type TestTree, TestLocation, decode_test_tree,
}

/// A file in the `test/` directory that likely contains tests.
pub type TestFile {
Expand Down Expand Up @@ -45,41 +47,43 @@ fn filepath_to_module_name(filepath: String) -> String {
|> string.replace(".gleam", "")
}

pub type NamedFunction =
#(String, fn() -> Dynamic)
pub type TestFunction {
TestFunction(module_name: String, name: String, body: fn() -> Dynamic)
}

pub fn identify_tests(
test_functions: List(NamedFunction),
test_functions: List(TestFunction),
ctx: Context,
) -> List(TestTree) {
) -> List(#(TestTree, TestLocation)) {
let #(standalone_tests, test_functions) =
test_functions
|> list.partition(is_standalone_test(_, ctx))
let standalone_tests =
standalone_tests
|> list.map(fn(named_fn) {
let #(function_name, function) = named_fn

|> list.map(fn(test_function) {
let function: fn() -> Nil =
function
test_function.body
|> dynamic.from
|> dynamic.unsafe_coerce

Test(function_name, function, False)
|> test_tree.Test
#(
Test(test_function.name, function, False)
|> test_tree.Test,
TestLocation(test_function.module_name),
)
})

let #(test_suites, _test_functions) =
test_functions
|> list.partition(is_test_suite(_, ctx))
let test_suites =
test_suites
|> list.filter_map(fn(named_fn) {
let #(_function_name, function) = named_fn

let value = function()

decode_test_tree(value)
|> list.filter_map(fn(test_function) {
test_function.body()
|> decode_test_tree
|> result.map(fn(test_tree) {
#(test_tree, TestLocation(test_function.module_name))
})
|> result.map_error(fn(error) {
logger.error(ctx.logger, string.inspect(error))
})
Expand All @@ -88,16 +92,12 @@ pub fn identify_tests(
list.concat([test_suites, standalone_tests])
}

fn is_standalone_test(named_fn: NamedFunction, ctx: Context) -> Bool {
let #(function_name, _) = named_fn

function_name
fn is_standalone_test(test_function: TestFunction, ctx: Context) -> Bool {
test_function.name
|> regex.check(with: ctx.config.discover_standalone_tests_pattern)
}

fn is_test_suite(named_fn: NamedFunction, ctx: Context) -> Bool {
let #(function_name, _) = named_fn

function_name
fn is_test_suite(test_function: TestFunction, ctx: Context) -> Bool {
test_function.name
|> regex.check(with: ctx.config.discover_describe_tests_pattern)
}
4 changes: 4 additions & 0 deletions src/startest/test_tree.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ pub type TestTree {
Test(Test)
}

pub type TestLocation {
TestLocation(module_name: String)
}

pub fn all_tests(tree: TestTree) -> List(#(String, Test)) {
collect_all_tests(tree, [], [])
}
Expand Down

0 comments on commit 7dea4a1

Please sign in to comment.