Skip to content

Commit

Permalink
Add a Clock to the Context and use it to get the current time
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdeviant committed Apr 21, 2024
1 parent 2d6005a commit a85c8c2
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 8 deletions.
1 change: 1 addition & 0 deletions gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ repository = { type = "github", user = "maxdeviant", repo = "startest" }

[dependencies]
argv = ">= 1.0.2 and < 2.0.0"
bigben = ">= 1.0.0 and < 2.0.0"
birl = ">= 1.6.1 and < 2.0.0"
exception = ">= 2.0.0 and < 3.0.0"
gleam_community_ansi = ">= 1.4.0 and < 2.0.0"
Expand Down
3 changes: 3 additions & 0 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

packages = [
{ name = "argv", version = "1.0.2", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "BA1FF0929525DEBA1CE67256E5ADF77A7CDDFE729E3E3F57A5BDCAA031DED09D" },
{ name = "bigben", version = "1.0.0", build_tools = ["gleam"], requirements = ["birl", "gleam_erlang", "gleam_otp", "gleam_stdlib"], otp_app = "bigben", source = "hex", outer_checksum = "8E5A98FA6E981EEEF016C40F1CDFADA095927CAF6CAAA0C7E295EED02FC95947" },
{ name = "birl", version = "1.6.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "ranger"], otp_app = "birl", source = "hex", outer_checksum = "976CFF85D34D50F7775896615A71745FBE0C325E50399787088F941B539A0497" },
{ name = "exception", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "exception", source = "hex", outer_checksum = "F5580D584F16A20B7FCDCABF9E9BE9A2C1F6AC4F9176FA6DD0B63E3B20D450AA" },
{ name = "filepath", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "EFB6FF65C98B2A16378ABC3EE2B14124168C0CE5201553DE652E2644DCFDB594" },
Expand All @@ -11,6 +12,7 @@ packages = [
{ name = "gleam_erlang", version = "0.25.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "054D571A7092D2A9727B3E5D183B7507DAB0DA41556EC9133606F09C15497373" },
{ name = "gleam_javascript", version = "0.8.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_javascript", source = "hex", outer_checksum = "14D5B7E1A70681E0776BF0A0357F575B822167960C844D3D3FA114D3A75F05A8" },
{ name = "gleam_json", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "thoas"], otp_app = "gleam_json", source = "hex", outer_checksum = "8B197DD5D578EA6AC2C0D4BDC634C71A5BCA8E7DB5F47091C263ECB411A60DF3" },
{ name = "gleam_otp", version = "0.10.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "0B04FE915ACECE539B317F9652CAADBBC0F000184D586AAAF2D94C100945D72B" },
{ name = "gleam_stdlib", version = "0.36.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "C0D14D807FEC6F8A08A7C9EF8DFDE6AE5C10E40E21325B2B29365965D82EB3D4" },
{ name = "glint", version = "1.0.0-rc1", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_community_colour", "gleam_stdlib", "snag"], otp_app = "glint", source = "hex", outer_checksum = "91EC8EFA3E8FBCB842C219AA02E8072204CEE02597B4C36C2ED78AD86DD1B2EC" },
{ name = "ranger", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "ranger", source = "hex", outer_checksum = "1566C272B1D141B3BBA38B25CB761EF56E312E79EC0E2DFD4D3C19FB0CC1F98C" },
Expand All @@ -22,6 +24,7 @@ packages = [

[requirements]
argv = { version = ">= 1.0.2 and < 2.0.0" }
bigben = { version = ">= 1.0.0 and < 2.0.0"}
birl = { version = ">= 1.6.1 and < 2.0.0" }
exception = { version = ">= 2.0.0 and < 3.0.0" }
gleam_community_ansi = { version = ">= 1.4.0 and < 2.0.0" }
Expand Down
3 changes: 2 additions & 1 deletion src/startest/cli.gleam
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//// The Startest command line interface (CLI).

import argv
import bigben/clock
import gleam/option.{None, Some}
import gleam/result
import gleam/string
Expand Down Expand Up @@ -59,7 +60,7 @@ pub fn run(config: Config) {
filter -> Some(filter)
})

let ctx = Context(config, logger: Logger)
let ctx = Context(config, clock: clock.new(), logger: Logger)

runner.run_tests(ctx)
})
Expand Down
3 changes: 2 additions & 1 deletion src/startest/context.gleam
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import bigben/clock.{type Clock}
import startest/config.{type Config}
import startest/logger.{type Logger}

/// The Startest context.
pub type Context {
Context(config: Config, logger: Logger)
Context(config: Config, clock: Clock, logger: Logger)
}
9 changes: 5 additions & 4 deletions src/startest/internal/runner/core.gleam
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//// The core test runner implementation.

import bigben/clock
import birl
import birl/duration
import gleam/int
Expand All @@ -19,7 +20,7 @@ import startest/test_failure
import startest/test_tree

pub fn run_tests(tests: List(TestFile), ctx: Context) {
let started_at = birl.utc_now()
let started_at = clock.now(ctx.clock)

let total_collect_duration =
tests
Expand Down Expand Up @@ -51,7 +52,7 @@ pub fn run_tests(tests: List(TestFile), ctx: Context) {
let test_count = list.length(tests)
logger.log(ctx.logger, "Running " <> int.to_string(test_count) <> " tests")

let execution_started_at = birl.utc_now()
let execution_started_at = clock.now(ctx.clock)

let executed_tests =
tests
Expand All @@ -65,7 +66,7 @@ pub fn run_tests(tests: List(TestFile), ctx: Context) {
})

let execution_duration =
birl.utc_now()
clock.now(ctx.clock)
|> birl.difference(execution_started_at)

let reporter_ctx = ReporterContext(logger: ctx.logger)
Expand All @@ -90,7 +91,7 @@ pub fn run_tests(tests: List(TestFile), ctx: Context) {
reporter.report_summary(ctx, failed_tests)

let total_duration =
birl.utc_now()
clock.now(ctx.clock)
|> birl.difference(started_at)

logger.log(
Expand Down
5 changes: 3 additions & 2 deletions src/startest/locator.gleam
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import bigben/clock
import birl
import birl/duration.{type Duration}
import gleam/dynamic.{type Dynamic}
Expand Down Expand Up @@ -94,7 +95,7 @@ fn identify_tests_in_file(
test_file: TestSourceFile,
ctx: Context,
) -> Result(TestFile, Nil) {
let started_at = birl.utc_now()
let started_at = clock.now(ctx.clock)

let #(standalone_tests, test_functions) =
test_file.tests
Expand Down Expand Up @@ -130,7 +131,7 @@ fn identify_tests_in_file(
[] -> Error(Nil)
tests -> {
let collect_duration =
birl.utc_now()
clock.now(ctx.clock)
|> birl.difference(started_at)

Ok(TestFile(
Expand Down

0 comments on commit a85c8c2

Please sign in to comment.