Skip to content

Commit

Permalink
move getTime from frameworkConfig to runConfig (anticipating combinin… (
Browse files Browse the repository at this point in the history
#132)

Summary:
…g TestFrameworks)
Pull Request resolved: #132

Differential Revision: D14569804

fbshipit-source-id: de0634eabb554e2d3e847f87377739ad427aa56b
  • Loading branch information
bandersongit authored and facebook-github-bot committed Mar 21, 2019
1 parent e12ef0f commit 319e8bb
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 24 deletions.
10 changes: 6 additions & 4 deletions src/rely/Rely.re
Expand Up @@ -31,6 +31,7 @@ module Describe = {
type describeConfig = {
updateSnapshots: bool,
snapshotDir: string,
getTime: unit => Time.t,
};

type describeUtils('ext) = {
Expand Down Expand Up @@ -149,7 +150,7 @@ module Make = (UserConfig: FrameworkConfig) => {
open Test;
open Describe;
open RunConfig;
let startTime = UserConfig.config.getTime();
let startTime = config.getTime();
let describeName = TestPath.(Describe(describePath) |> toString);

let testHashes = state.testHashes;
Expand Down Expand Up @@ -310,7 +311,7 @@ module Make = (UserConfig: FrameworkConfig) => {
let runTest = () => {
let timingInfo =
Util.time(
UserConfig.config.getTime,
config.getTime,
() => {
let _ =
switch (usersTest(testUtils)) {
Expand Down Expand Up @@ -424,7 +425,7 @@ module Make = (UserConfig: FrameworkConfig) => {

let describeResult = {
path: describePath,
endTime: Some(UserConfig.config.getTime()),
endTime: Some(config.getTime()),
startTime: Some(startTime),
describeResults: childDescribeResults^,
testResults,
Expand Down Expand Up @@ -516,7 +517,7 @@ module Make = (UserConfig: FrameworkConfig) => {
let run = (config: RunConfig.t) =>
useIsRunning(() =>
Util.withBacktrace(() => {
let startTime = UserConfig.config.getTime();
let startTime = config.getTime();
let notifyReporters = f => List.iter(f, config.reporters);
let reporterTestSuites =
testSuites^
Expand Down Expand Up @@ -549,6 +550,7 @@ module Make = (UserConfig: FrameworkConfig) => {
~config={
updateSnapshots: config.updateSnapshots,
snapshotDir: UserConfig.config.snapshotDir,
getTime: config.getTime
},
~state=acc.testRunState,
~describePath=Terminal(name),
Expand Down
2 changes: 1 addition & 1 deletion src/rely/Rely.rei
Expand Up @@ -39,6 +39,7 @@ module RunConfig: {
let onTestFrameworkFailure: (unit => unit, t) => t;
let updateSnapshots: (bool, t) => t;
let withReporters: (list(reporter), t) => t;
let internal_do_not_use_get_time: (unit => Time.t, t) => t;
};

module MatcherUtils: {
Expand Down Expand Up @@ -92,7 +93,6 @@ module TestFrameworkConfig: {
type t;
let initialize: requiredConfiguration => t;
let withMaxNumberOfMockCalls: (int, t) => t;
let internal_do_not_use_get_time: (unit => Time.t, t) => t;
};

module type FrameworkConfig = {let config: TestFrameworkConfig.t;};
Expand Down
4 changes: 4 additions & 0 deletions src/rely/RunConfig.re
Expand Up @@ -16,6 +16,7 @@ module RunConfig = {
updateSnapshots: bool,
onTestFrameworkFailure: unit => unit,
reporters: list(Reporter.t),
getTime: unit => Time.t,
};

let initialize = () => {
Expand All @@ -29,6 +30,7 @@ module RunConfig = {
flush,
}),
],
getTime: Clock.getTime,
};

type reporter =
Expand Down Expand Up @@ -58,6 +60,8 @@ module RunConfig = {
{...config, reporters};
};

let internal_do_not_use_get_time = (fn, cfg: t) => {...cfg, getTime: fn};

let onTestFrameworkFailure = (onTestFrameworkFailure, config) => {
...config,
onTestFrameworkFailure,
Expand Down
3 changes: 0 additions & 3 deletions src/rely/TestFrameworkConfig.re
Expand Up @@ -16,7 +16,6 @@ module TestFrameworkConfig = {
snapshotDir: string,
projectDir: string,
maxNumMockCalls: int,
getTime: unit => Time.t,
};

type optionalConfiguration = {getTime: unit => Time.t};
Expand All @@ -25,11 +24,9 @@ module TestFrameworkConfig = {
config => {
snapshotDir: config.snapshotDir,
projectDir: config.projectDir,
getTime: Clock.getTime,
maxNumMockCalls: Mock.defaultNumMaxCalls,
};

let internal_do_not_use_get_time = (fn, cfg: t) => {...cfg, getTime: fn};
let withMaxNumberOfMockCalls = (num: int, cfg: t) => {
...cfg,
maxNumMockCalls: num,
Expand Down
25 changes: 14 additions & 11 deletions tests/TestRunnerOutputSnapshotTest.re
Expand Up @@ -27,16 +27,6 @@ module MakeTestFramework = (SnapshotDir: SnapshotDir) : Rely.TestFramework =>
),
projectDir: "",
})
/* evil hack, default reporter not currently exposed and time can't be
* passed to reporters, setting time in the future by a second (which is longer
* than any tests should take so that when the terminal reporter calculates
* how long tests took to run it gets a negative number which gets "rounded"
* to < 1 ms. Come either virtual modules/the next major version we can fix this,
* until then, I think this is the most reasonable option to ensure our
* snapshots are consistent */
|> internal_do_not_use_get_time(() =>
Seconds(Unix.gettimeofday() +. 1000.)
)
);
});

Expand Down Expand Up @@ -78,7 +68,20 @@ let testRunnerOutputSnapshotTest =
testFn(utils)
);
TestFramework.run(
Rely.RunConfig.(initialize() |> updateSnapshots(doUpdate)),
Rely.RunConfig.(
initialize()
|> updateSnapshots(doUpdate)
/* evil hack, default reporter not currently exposed and time can't be
* passed to reporters, setting time in the future by a second (which is longer
* than any tests should take so that when the terminal reporter calculates
* how long tests took to run it gets a negative number which gets "rounded"
* to < 1 ms. Come either virtual modules/the next major version we can fix this,
* until then, I think this is the most reasonable option to ensure our
* snapshots are consistent */
|> internal_do_not_use_get_time(() =>
Seconds(Unix.gettimeofday() +. 1000.)
)
),
);
();
})
Expand Down
12 changes: 7 additions & 5 deletions tests/__tests__/rely/TestSuiteRunner.re
Expand Up @@ -19,10 +19,12 @@ let run = (testSuites: list(TestSuite.t), reporter: Rely.Reporter.t) => {

testSuites
|> List.map(TestSuite.toFunction)
|> List.iter(ts => ts(
~describe=TestFramework.describe,
~describeSkip=TestFramework.describeSkip,
));
|> List.iter(ts =>
ts(
~describe=TestFramework.describe,
~describeSkip=TestFramework.describeSkip,
)
);

TestFramework.run(
Rely.RunConfig.(
Expand All @@ -39,7 +41,6 @@ let runWithCustomTime = (getTime, testSuites, reporter) => {
let config =
Rely.TestFrameworkConfig.(
initialize({snapshotDir: "unused", projectDir: ""})
|> internal_do_not_use_get_time(getTime)
);
});

Expand All @@ -57,6 +58,7 @@ let runWithCustomTime = (getTime, testSuites, reporter) => {
initialize()
|> withReporters([Custom(reporter)])
|> onTestFrameworkFailure(() => ())
|> internal_do_not_use_get_time(getTime)
),
);
};

0 comments on commit 319e8bb

Please sign in to comment.