Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using generator in givePort() #2040

Merged
merged 10 commits into from
Sep 13, 2024
22 changes: 8 additions & 14 deletions tests/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import { map } from "ramda";
import { map, when, equals, nAry } from "ramda";
import { z } from "zod";
import { ezFileBrand } from "../src/file-schema";
import { SchemaHandler, walkSchema } from "../src/schema-walker";

let lastGivenPort = 8010;
const reservedPorts = {
example: 8090,
};
export const givePort = (test?: keyof typeof reservedPorts) => {
if (test && reservedPorts[test]) {
return reservedPorts[test];
}
do {
lastGivenPort++;
} while (Object.values(reservedPorts).includes(lastGivenPort));
return lastGivenPort;
};
const disposer = (function* () {
let port = 8010;
while (true) yield port++;
})();

export const givePort = (test?: "example", rsvd = 8090): number =>
test ? rsvd : when(equals(rsvd), nAry(0, givePort))(disposer.next().value);

export const serializeSchemaForTest = (
subject: z.ZodTypeAny,
Expand Down