Skip to content

Commit

Permalink
Return more structured data from locator.locate_test_files
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdeviant committed Apr 21, 2024
1 parent dea81a6 commit ee29579
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 28 deletions.
13 changes: 4 additions & 9 deletions src/startest/internal/runner/backend/erlang.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ pub fn run_tests(ctx: Context) -> Nil {

let tests =
test_files
|> list.map(fn(filepath) {
|> list.map(fn(test_file) {
let erlang_module_name =
filepath
test_file.module_name
|> gleam_module_name_to_erlang_module_name
|> binary_to_atom

Expand All @@ -38,13 +38,8 @@ pub fn run_tests(ctx: Context) -> Nil {
}

@target(erlang)
fn gleam_module_name_to_erlang_module_name(filepath: String) -> String {
filepath
|> string.slice(
at_index: string.length("test/"),
length: string.length(filepath),
)
|> string.replace(".gleam", "")
fn gleam_module_name_to_erlang_module_name(module_name: String) -> String {
module_name
|> string.replace("/", "@")
}

Expand Down
16 changes: 2 additions & 14 deletions src/startest/internal/runner/backend/javascript.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import gleam/javascript/promise.{type Promise}
@target(javascript)
import gleam/list
@target(javascript)
import gleam/string
@target(javascript)
import startest/context.{type Context}
@target(javascript)
import startest/internal/gleam_toml
Expand All @@ -26,9 +24,9 @@ pub fn run_tests(ctx: Context) -> Promise(Nil) {

use tests <- promise.await(
test_files
|> list.map(fn(filepath) {
|> list.map(fn(test_file) {
let js_module_path =
"../" <> package_name <> "/" <> gleam_filepath_to_mjs_filepath(filepath)
"../" <> package_name <> "/" <> test_file.module_name <> ".mjs"

get_exports(js_module_path)
|> promise.map(array.to_list)
Expand All @@ -43,16 +41,6 @@ pub fn run_tests(ctx: Context) -> Promise(Nil) {
|> promise.resolve
}

@target(javascript)
fn gleam_filepath_to_mjs_filepath(filepath: String) {
filepath
|> string.slice(
at_index: string.length("test/"),
length: string.length(filepath),
)
|> string.replace(".gleam", ".mjs")
}

@target(javascript)
@external(javascript, "../../../../startest_ffi.mjs", "get_exports")
fn get_exports(
Expand Down
32 changes: 27 additions & 5 deletions src/startest/locator.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,40 @@ import startest/logger
import startest/test_case.{type Test, Test}
import startest/test_tree.{type TestTree, decode_test_tree}

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

/// Returns the list of files in the `test/` directory.
pub fn locate_test_files() -> Result(List(String), Nil) {
pub fn locate_test_files() -> Result(List(TestFile), Nil) {
use test_files <- try(
simplifile.get_files(in: "test")
|> result.nil_error,
)

let gleam_test_files =
test_files
|> list.filter(fn(filename) { string.ends_with(filename, ".gleam") })
test_files
|> list.filter(fn(filepath) { string.ends_with(filepath, ".gleam") })
|> list.map(fn(filepath) {
let module_name = filepath_to_module_name(filepath)
TestFile(filepath, module_name)
})
|> Ok
}

Ok(gleam_test_files)
/// Returns the Gleam module name from the given filepath.
fn filepath_to_module_name(filepath: String) -> String {
filepath
|> string.slice(
at_index: string.length("test/"),
length: string.length(filepath),
)
|> string.replace(".gleam", "")
}

pub type NamedFunction =
Expand Down

0 comments on commit ee29579

Please sign in to comment.