Skip to content

Commit

Permalink
Migrate foundry-sdk-generators e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ericanderson committed Jul 31, 2024
1 parent 4a5b12f commit f789f73
Show file tree
Hide file tree
Showing 30 changed files with 173 additions and 45 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ packages/client/src/generatedNoCheck
packages/client.test.ontology/src/generatedNoCheck
packages/legacy-client/src/generatedNoCheck
packages/create-app.template.*/src/generatedNoCheck
packages/e2e.test.foundry-sdk-generator/src/generatedNoCheck

.log
pnpm-publish-summary.json
Expand Down
13 changes: 11 additions & 2 deletions .monorepolint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const nonStandardPackages = [
"@osdk/e2e.sandbox.todoapp",
"@osdk/examples.*",
"@osdk/foundry-sdk-generator",
"@osdk/e2e.test.foundry-sdk-generator",
"@osdk/monorepo.*", // internal monorepo packages
"@osdk/shared.client", // hand written package that only exposes a symbol
"@osdk/tests.*",
Expand Down Expand Up @@ -241,7 +242,7 @@ function getTsconfigOptions(baseTsconfigPath, opts) {
* legacy: boolean,
* esmOnly?: boolean,
* customTsconfigExcludes?: string[],
* tsVersion?: "^5.5.2"|"^4.9.5",
* tsVersion?: typeof LATEST_TYPESCRIPT_DEP | "^4.9.5",
* skipTsconfigReferences?: boolean
* singlePackageName?: string
* }} options
Expand Down Expand Up @@ -491,7 +492,15 @@ NOTE: DO NOT EDIT THIS README BY HAND. It is generated by monorepolint.
legacy: false,
tsVersion: LATEST_TYPESCRIPT_DEP,
customTsconfigExcludes: [
"./src/__e2e_tests__/**/**.test.ts",
"./src/generatedNoCheck/**/*",
],
}),
...standardPackageRules({
includePackages: ["@osdk/e2e.test.foundry-sdk-generator"],
}, {
legacy: false,
tsVersion: LATEST_TYPESCRIPT_DEP,
customTsconfigExcludes: [
"./src/generatedNoCheck/**/*",
],
}),
Expand Down
80 changes: 67 additions & 13 deletions ...-generator/src/__e2e_tests__/testSetup.ts → ...dry-sdk-generator/generateMockOntology.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 Palantir Technologies, Inc. All rights reserved.
* Copyright 2024 Palantir Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,21 +14,29 @@
* limitations under the License.
*/

// @ts-check
import { __testSeamOnly_NotSemverStable__GeneratePackageCommand as GeneratePackageCommand } from "@osdk/foundry-sdk-generator";
import { apiServer } from "@osdk/shared.test";
import { rm } from "fs/promises";
import { join } from "path";
import { GeneratePackageCommand } from "../generate/index.js";
import * as fs from "node:fs/promises";
import * as path from "node:path";
import { fileURLToPath } from "node:url";

const dir = `${__dirname}/../generatedNoCheck/`;
export async function setup() {
const dir = path.join(
path.dirname(fileURLToPath(import.meta.url)),
"src",
"generatedNoCheck",
);
async function setup() {
apiServer.listen();

try {
await rm(join(dir, "@test-app"), { recursive: true });
await rm(join(dir, "@test-app2"), { recursive: true });
} catch (e) {
// Only needed for regenerations
}
const testAppDir = path.join(dir, "@test-app");
const testApp2Dir = path.join(dir, "@test-app2");

await rmRf(testAppDir);
await rmRf(testApp2Dir);

await safeStat(testAppDir, "should not exist");
await safeStat(testApp2Dir, "should not exist");

const generatePackageCommand = new GeneratePackageCommand();
await generatePackageCommand.handler({
Expand Down Expand Up @@ -69,6 +77,8 @@ export async function setup() {
$0: "",
});

await safeStat(testAppDir, "should exist");

await generatePackageCommand.handler({
packageName: "@test-app2/osdk",
packageVersion: "0.0.1",
Expand Down Expand Up @@ -107,10 +117,54 @@ export async function setup() {
_: [],
$0: "",
});

await safeStat(testApp2Dir, "should exist");
}

export async function teardown() {
// eslint-disable-next-line no-console
console.log("Test teardown: stopping API server");
console.log("teardown: stopping API server");
apiServer.close();
}

await setup();
await teardown();

/**
* @param {string} testAppDir
*/
async function rmRf(testAppDir) {
try {
await fs.rm(testAppDir, { recursive: true });
} catch (e) {
// eslint-disable-next-line no-console
console.debug("rm error", e);
// Only needed for regenerations
}
}

/**
* @param {string} filePath
* @param {"should exist" | "should not exist"} type
* @returns
*/
async function safeStat(filePath, type) {
try {
const ret = await fs.stat(filePath);
if (type !== "should exist") {
throw new Error(`Expected ${filePath} to not exist`);
}

// eslint-disable-next-line no-console
console.log(`safeStat: ${filePath} exists`);
return ret;
} catch (e) {
if (type === "should exist") {
throw new Error(`Expected ${filePath} to exist`);
}

// eslint-disable-next-line no-console
console.log(`safeStat: ${filePath} does not exist`);
return undefined;
}
}
3 changes: 3 additions & 0 deletions packages/e2e.test.foundry-sdk-generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"check-attw": "monorepo.tool.attw both",
"check-spelling": "cspell --quiet .",
"clean": "rm -rf lib dist types build tsconfig.tsbuildinfo",
"codegen": "node ./generateMockOntology.js",
"fix-lint": "eslint . --fix && dprint fmt --config $(find-up dprint.json)",
"lint": "eslint . && dprint check --config $(find-up dprint.json)",
"test": "vitest run --pool=forks",
Expand All @@ -34,9 +35,11 @@
"execa": "^9.3.0"
},
"devDependencies": {
"@osdk/legacy-client": "workspace:~",
"@osdk/monorepo.api-extractor": "workspace:~",
"@osdk/monorepo.tsconfig": "workspace:~",
"@osdk/monorepo.tsup": "workspace:~",
"@osdk/shared.test": "workspace:~",
"typescript": "^5.5.4"
},
"publishConfig": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@
* limitations under the License.
*/

import { __testSeamOnly_NotSemverStable__GeneratePackageCommand as GeneratePackageCommand } from "@osdk/foundry-sdk-generator";
import { existsSync } from "node:fs";
import * as fs from "node:fs/promises";
import * as path from "node:path";
import { fileURLToPath } from "node:url";
import { describe, expect, it } from "vitest";
import { GeneratePackageCommand } from "./GeneratePackageCommand.js";

describe(GeneratePackageCommand, () => {
// ensure that we do not break backcompat by retaining our scripts export that links to the bundled types and esm output
it("has a public scripts export", async () => {
const generatedPath = path.join(
__dirname,
"..",
path.dirname(fileURLToPath(import.meta.url)),
"generatedNoCheck",
"@test-app",
"osdk",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { apiServer, loadAll, stubData } from "@osdk/shared.test";
import {
afterAll,
beforeAll,
Expand All @@ -39,8 +40,6 @@ import type {
Page,
Result,
} from "../generatedNoCheck/@test-app/osdk/index.js";

import { apiServer, loadAll, stubData } from "@osdk/shared.test";
import type {
Employee,
Office,
Expand Down Expand Up @@ -123,7 +122,7 @@ describe("LoadObjects", () => {

const emp = assertOkOrThrow(result);

expectTypeOf(emp).toEqualTypeOf<{
expectTypeOf(emp).branded.toEqualTypeOf<{
readonly fullName: string | undefined;
readonly __primaryKey: number;
readonly __apiName: "Employee";
Expand All @@ -141,7 +140,7 @@ describe("LoadObjects", () => {

const emp = assertOkOrThrow(result);

expectTypeOf(emp).toEqualTypeOf<{
expectTypeOf(emp).branded.toEqualTypeOf<{
readonly fullName: string | undefined;
readonly __primaryKey: number;
readonly __apiName: "Employee";
Expand All @@ -157,7 +156,7 @@ describe("LoadObjects", () => {
const emp = await client.ontology.objects.Employee.select(["fullName"])
.fetchOne(stubData.employee1.__primaryKey);

expectTypeOf(emp).toEqualTypeOf<{
expectTypeOf(emp).branded.toEqualTypeOf<{
readonly fullName: string | undefined;
readonly __primaryKey: number;
readonly __apiName: "Employee";
Expand Down Expand Up @@ -500,7 +499,7 @@ describe("LoadObjects", () => {
.all();
const employees = assertOkOrThrow(result);
expect(employees.length).toEqual(3);
expectTypeOf(employees[0]).toEqualTypeOf<{
expectTypeOf(employees[0]).branded.toEqualTypeOf<{
readonly fullName: string | undefined;
readonly __primaryKey: number;
readonly __apiName: "Employee";
Expand All @@ -515,7 +514,7 @@ describe("LoadObjects", () => {
.asyncIter(),
);
expect(employees.length).toEqual(3);
expectTypeOf(employees[0]).toEqualTypeOf<{
expectTypeOf(employees[0]).branded.toEqualTypeOf<{
readonly fullName: string | undefined;
readonly __primaryKey: number;
readonly __apiName: "Employee";
Expand All @@ -529,7 +528,7 @@ describe("LoadObjects", () => {
.fetchPageWithErrors({ pageSize: 2 });
const employees = assertOkOrThrow(result);
expect(employees.data.length).toEqual(2);
expectTypeOf(employees.data[0]).toEqualTypeOf<{
expectTypeOf(employees.data[0]).branded.toEqualTypeOf<{
readonly fullName: string | undefined;
readonly __primaryKey: number;
readonly __apiName: "Employee";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { apiServer, loadAll } from "@osdk/shared.test";
import {
afterAll,
beforeAll,
Expand All @@ -35,8 +36,6 @@ import type {
Result,
SearchObjectsError,
} from "../generatedNoCheck/@test-app/osdk/index.js";

import { apiServer, loadAll } from "@osdk/shared.test";
import type {
Employee,
Office,
Expand Down Expand Up @@ -428,10 +427,12 @@ describe("SearchObjects", () => {
const employees = assertOkOrThrow(result);
expect(employees.data.length).toEqual(1);
expect(employees.data[0].fullName).toEqual("John Doe");
expectTypeOf(employees.data[0]).toEqualTypeOf<{
expectTypeOf(employees.data[0]).branded.toEqualTypeOf<{
readonly fullName: string | undefined;
readonly __primaryKey: number;
readonly __apiName: "Employee";
readonly $primaryKey: number;
readonly $apiName: "Employee";
}>();
});

Expand All @@ -446,10 +447,12 @@ describe("SearchObjects", () => {
const employees = assertOkOrThrow(result);
expect(employees.data.length).toEqual(1);
expect(employees.data[0].fullName).toEqual("John Doe");
expectTypeOf(employees.data[0]).toEqualTypeOf<{
expectTypeOf(employees.data[0]).branded.toEqualTypeOf<{
readonly fullName: string | undefined;
readonly __primaryKey: number;
readonly __apiName: "Employee";
readonly $primaryKey: number;
readonly $apiName: "Employee";
}>();
});

Expand Down
3 changes: 3 additions & 0 deletions packages/e2e.test.foundry-sdk-generator/tsconfig.cjs.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
"include": [
"./src/**/*"
],
"exclude": [
"./src/generatedNoCheck/**/*"
],
"references": []
}
3 changes: 3 additions & 0 deletions packages/e2e.test.foundry-sdk-generator/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
"include": [
"./src/**/*"
],
"exclude": [
"./src/generatedNoCheck/**/*"
],
"references": []
}
12 changes: 11 additions & 1 deletion packages/e2e.test.foundry-sdk-generator/turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@
"extends": ["//"],
"tasks": {
"test": {
"dependsOn": ["@osdk/foundry-sdk-generator#transpile"]
"dependsOn": ["@osdk/foundry-sdk-generator#transpile", "codegen"]
},
"codegen": {
"inputs": ["generateMockOntology.js"],
"outputs": ["src/generatedNoCheck/**/*"],
"dependsOn": [
"@osdk/foundry-sdk-generator#transpile",
"@osdk/foundry-sdk-generator#typecheck",
"@osdk/shared.test#transpile",
"@osdk/shared.test#typecheck"
]
}
}
}
33 changes: 33 additions & 0 deletions packages/e2e.test.foundry-sdk-generator/vitest.config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2024 Palantir Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { dirname, join } from "path";
import { fileURLToPath } from "url";
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
alias: {
"consola": join(
dirname(fileURLToPath(import.meta.url)),
"src",
"legacy-client",
"consola.ts",
),
},
pool: "forks",
},
});
Loading

0 comments on commit f789f73

Please sign in to comment.