Skip to content

Commit

Permalink
test: use more precise time comparison (#5268)
Browse files Browse the repository at this point in the history
* test: use more precise time comparison

This approach covers the edge case
when 2 numeric values are in the same precision vicinity,
but get rounded to the different values.

E.g. `a = 0.514` and `b = 0.516`.
The diff is `0.002`,
but the diff between rounded values was `0.01` before this fix.

This is a tiny fix, but without it, the CI fails from time to time.
That could cost ~35m of waiting time in the merge queue.

* chore: sorted imports
  • Loading branch information
vvagaytsev authored Oct 18, 2023
1 parent 1c5aa87 commit e256812
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
9 changes: 8 additions & 1 deletion core/src/util/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import { GlobalOptions, globalOptions, ParameterValues } from "../cli/params"
import cloneDeep from "fast-copy"
import { isEqual, keyBy, set, mapValues } from "lodash"
import { isEqual, keyBy, set, mapValues, round } from "lodash"
import { Garden, GardenOpts, GardenParams, GetConfigGraphParams, resolveGardenParams } from "../garden"
import { DeepPrimitiveMap, StringMap } from "../config/common"
import { ModuleConfig } from "../config/module"
Expand Down Expand Up @@ -534,3 +534,10 @@ export function captureStream(stream: NodeJS.WritableStream) {
},
}
}

export function equalWithPrecision(a: number, b: number, precision: number): boolean {
const rawDiff = Math.abs(a - b)
const diff = round(rawDiff, precision)
const eps = Math.pow(10, -precision)
return diff <= eps
}
15 changes: 8 additions & 7 deletions core/test/unit/src/build-staging/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@

import { join } from "path"
import {
FileStatsHelper,
cloneFileAsync,
ExtendedStats,
FileStatsHelper,
MappedPaths,
ResolveSymlinkParams,
cloneFileAsync,
scanDirectoryForClone,
MappedPaths,
} from "../../../../src/build-staging/helpers"
import { TempDirectory, makeTempDir } from "../../../../src/util/fs"
import { realpath, symlink, writeFile, readFile, mkdir, ensureFile, ensureDir } from "fs-extra"
import { makeTempDir, TempDirectory } from "../../../../src/util/fs"
import { ensureDir, ensureFile, mkdir, readFile, realpath, symlink, writeFile } from "fs-extra"
import { expect } from "chai"
import { expectError } from "../../../helpers"
import { sleep } from "../../../../src/util/util"
import { round, sortBy } from "lodash"
import { sortBy } from "lodash"
import { equalWithPrecision } from "../../../../src/util/testing"

describe("build staging helpers", () => {
let statsHelper: FileStatsHelper
Expand Down Expand Up @@ -86,7 +87,7 @@ describe("build staging helpers", () => {
const statA = await statsHelper.extendedStat({ path: a })
const statB = await statsHelper.extendedStat({ path: b })

expect(round(statA?.mtimeMs!, 2)).to.equal(round(statB?.mtimeMs!, 2))
expect(equalWithPrecision(statA?.mtimeMs!, statB?.mtimeMs!, 2)).to.be.true
})

it("skips if file at target exists and has same mtime and size", async () => {
Expand Down

0 comments on commit e256812

Please sign in to comment.