Skip to content

Commit

Permalink
Group tests by file when collecting them
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdeviant committed Apr 21, 2024
1 parent 93b802c commit 91a76a6
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 41 deletions.
24 changes: 15 additions & 9 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.{TestFunction}
import startest/locator.{TestFunction, TestSourceFile}

@target(erlang)
pub fn run_tests(ctx: Context) -> Nil {
Expand All @@ -27,16 +27,22 @@ pub fn run_tests(ctx: Context) -> Nil {
|> gleam_module_name_to_erlang_module_name
|> binary_to_atom

get_exports(erlang_module_name)
|> list.map(fn(pair) {
let #(name, body) =
pair
|> pair.map_first(atom_to_binary)
let tests =
get_exports(erlang_module_name)
|> 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)
})
TestFunction(
module_name: test_file.module_name,
name: name,
body: body,
)
})

TestSourceFile(..test_file, tests: tests)
})
|> list.flatten
|> locator.identify_tests(ctx)

tests
Expand Down
4 changes: 2 additions & 2 deletions 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.{TestFunction}
import startest/locator.{TestFunction, TestSourceFile}

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

TestFunction(module_name: test_file.module_name, name: name, body: body)
}))
|> promise.map(fn(tests) { TestSourceFile(..test_file, tests: tests) })
})
|> promise.await_list
|> promise.map(list.flatten)
|> promise.map(locator.identify_tests(_, ctx)),
)

Expand Down
14 changes: 7 additions & 7 deletions src/startest/internal/runner/core.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,28 @@ import gleam/option.{None, Some}
import gleam/string
import startest/context.{type Context}
import startest/internal/process
import startest/locator.{type TestFile}
import startest/logger
import startest/reporter
import startest/reporters.{ReporterContext}
import startest/test_case.{
type Test, type TestOutcome, ExecutedTest, Failed, Passed, Skipped,
}
import startest/test_failure
import startest/test_tree.{type TestLocation, type TestTree}
import startest/test_tree

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

let tests =
tests
|> list.flat_map(fn(tree_with_location) {
let #(tree, location) = tree_with_location

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

#(location.module_name, full_test_name, test_case)
#(test_file.module_name, full_test_name, test_case)
})
})
|> list.filter(fn(tuple) {
Expand Down
68 changes: 49 additions & 19 deletions src/startest/locator.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,34 @@ import simplifile
import startest/context.{type Context}
import startest/logger
import startest/test_case.{type Test, Test}
import startest/test_tree.{
type TestLocation, type TestTree, TestLocation, decode_test_tree,
}
import startest/test_tree.{type TestTree, decode_test_tree}

/// A file in the `test/` directory that likely contains tests.
/// A file that contains tests.
pub type TestFile {
TestFile(
/// The name of the Gleam module.
module_name: String,
/// The filepath to the `.gleam` file.
filepath: String,
/// The list of tests in the file.
tests: List(TestTree),
)
}

/// A file in the `test/` directory that likely contains tests.
pub type TestSourceFile {
TestSourceFile(
/// The name of the Gleam module.
module_name: String,
/// The filepath to the `.gleam` file.
filepath: String,
/// The list of tests in the file.
tests: List(TestFunction),
)
}

/// Returns the list of files in the `test/` directory.
pub fn locate_test_files(ctx: Context) -> Result(List(TestFile), Nil) {
pub fn locate_test_files(ctx: Context) -> Result(List(TestSourceFile), Nil) {
use test_files <- try(
simplifile.get_files(in: "test")
|> result.nil_error,
Expand All @@ -40,8 +52,11 @@ pub fn locate_test_files(ctx: Context) -> Result(List(TestFile), Nil) {
}
})
|> list.map(fn(filepath) {
let module_name = filepath_to_module_name(filepath)
TestFile(filepath, module_name)
TestSourceFile(
module_name: filepath_to_module_name(filepath),
filepath: filepath,
tests: [],
)
})
|> Ok
}
Expand All @@ -60,12 +75,23 @@ pub type TestFunction {
TestFunction(module_name: String, name: String, body: fn() -> Dynamic)
}

/// Identifies all of the tests contained in the given list of `TestSourceFile`s.
///
/// Any files that don't have any tests will be excluded from the result.
pub fn identify_tests(
test_functions: List(TestFunction),
test_files: List(TestSourceFile),
ctx: Context,
) -> List(TestFile) {
test_files
|> list.filter_map(identify_tests_in_file(_, ctx))
}

fn identify_tests_in_file(
test_file: TestSourceFile,
ctx: Context,
) -> List(#(TestTree, TestLocation)) {
) -> Result(TestFile, Nil) {
let #(standalone_tests, test_functions) =
test_functions
test_file.tests
|> list.partition(is_standalone_test(_, ctx))
let standalone_tests =
standalone_tests
Expand All @@ -75,11 +101,8 @@ pub fn identify_tests(
|> dynamic.from
|> dynamic.unsafe_coerce

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

let #(test_suites, _test_functions) =
Expand All @@ -90,15 +113,22 @@ pub fn identify_tests(
|> 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))
})
})

list.concat([test_suites, standalone_tests])
let tests = list.concat([test_suites, standalone_tests])

case tests {
[] -> Error(Nil)
tests ->
Ok(TestFile(
module_name: test_file.module_name,
filepath: test_file.filepath,
tests: tests,
))
}
}

fn is_standalone_test(test_function: TestFunction, ctx: Context) -> Bool {
Expand Down
4 changes: 0 additions & 4 deletions src/startest/test_tree.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ 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 91a76a6

Please sign in to comment.