Skip to content

Commit

Permalink
Fix crash in auxiliary program creation
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbranch committed Dec 3, 2023
1 parent a210461 commit 57a08de
Show file tree
Hide file tree
Showing 6 changed files with 63,843 additions and 15 deletions.
7 changes: 6 additions & 1 deletion packages/cli/test/snapshots.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import assert from "node:assert";
import { after, describe, test } from "node:test";

const attw = `node ${new URL("../../dist/index.js", import.meta.url).pathname}`;
const updateSnapshots = process.env.UPDATE_SNAPSHOTS;
const updateSnapshots = process.env.UPDATE_SNAPSHOTS || process.env.U;
const testFilter = (process.env.TEST_FILTER || process.env.T)?.toLowerCase();

const tests = [
["@apollo__client-3.7.16.tgz"],
Expand Down Expand Up @@ -60,6 +61,10 @@ describe("snapshots", async () => {

for (const [tarball, options] of tests) {
const fixture = tarball + (options ? ` ${options}` : "");
if (testFilter && !fixture.toLowerCase().includes(testFilter)) {
continue;
}

test(fixture, async () => {
const tarballPath = new URL(`../../../core/test/fixtures/${tarball}`, import.meta.url).pathname;
let stdout;
Expand Down
30 changes: 19 additions & 11 deletions packages/core/src/createPackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class Package {
packageName: string,
packageVersion: string,
resolvedUrl?: string,
typesPackage?: Package["typesPackage"]
typesPackage?: Package["typesPackage"],
) {
this.#files = files;
this.packageName = packageName;
Expand All @@ -29,10 +29,10 @@ export class Package {
this.typesPackage = typesPackage;
}

readFile(path: string): string {
tryReadFile(path: string): string | undefined {
const file = this.#files[path];
if (file === undefined) {
throw new Error(`File not found: ${path}`);
return undefined;
}
if (typeof file === "string") {
return file;
Expand All @@ -42,6 +42,14 @@ export class Package {
return content;
}

readFile(path: string): string {
const content = this.tryReadFile(path);
if (content === undefined) {
throw new Error(`File not found: ${path}`);
}
return content;
}

fileExists(path: string): boolean {
return path in this.#files;
}
Expand Down Expand Up @@ -91,7 +99,7 @@ export interface CreatePackageFromNpmOptions {

export async function createPackageFromNpm(
packageSpec: string,
{ definitelyTyped = true, before }: CreatePackageFromNpmOptions = {}
{ definitelyTyped = true, before }: CreatePackageFromNpmOptions = {},
): Promise<Package> {
const parsed = parsePackageSpec(packageSpec);
if (parsed.status === "error") {
Expand Down Expand Up @@ -125,7 +133,7 @@ export async function createPackageFromNpm(
version: definitelyTyped,
},
],
before
before,
);
}

Expand All @@ -138,7 +146,7 @@ export async function createPackageFromNpm(
export async function resolveTypesPackageForPackage(
packageName: string,
packageVersion: string,
before?: Date
before?: Date,
): Promise<{ packageName: string; packageVersion: string; tarballUrl: string } | undefined> {
const typesPackageName = ts.getTypesPackageName(packageName);
try {
Expand All @@ -162,18 +170,18 @@ export async function resolveTypesPackageForPackage(
version: "latest",
},
],
before
before,
)),
};
} catch {}
}

async function getNpmTarballUrl(
packageSpecs: readonly ParsedPackageSpec[],
before?: Date
before?: Date,
): Promise<{ tarballUrl: string; packageVersion: string }> {
const fetchPackument = packageSpecs.some(
(spec) => spec.versionKind === "range" || (spec.versionKind === "tag" && spec.version !== "latest")
(spec) => spec.versionKind === "range" || (spec.versionKind === "tag" && spec.version !== "latest"),
);
const packumentUrl = `https://registry.npmjs.org/${packageSpecs[0].name}`;
const includeTimes = before !== undefined && packageSpecs.some((spec) => spec.versionKind !== "exact");
Expand All @@ -193,9 +201,9 @@ async function getNpmTarballUrl(
if (packageSpec.versionKind === "range") {
packageVersion = maxSatisfying(
Object.keys(doc.versions).filter(
(v) => !doc.versions[v].deprecated && (!before || !doc.time || new Date(doc.time[v]) <= before)
(v) => !doc.versions[v].deprecated && (!before || !doc.time || new Date(doc.time[v]) <= before),
),
packageSpec.version
packageSpec.version,
);
if (!packageVersion) {
continue;
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/multiCompilerHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,11 @@ export class CompilerHostWrapper {
if (cached) {
return cached;
}
const content = fileName.startsWith("/node_modules/typescript/lib") ? "" : fs.readFile(fileName);
const content = fileName.startsWith("/node_modules/typescript/lib") ? "" : fs.tryReadFile(fileName);
if (content === undefined) {
return undefined;
}

const sourceFile = ts.createSourceFile(
fileName,
content,
Expand Down
Binary file added packages/core/test/fixtures/next@14.0.3.tgz
Binary file not shown.
10 changes: 8 additions & 2 deletions packages/core/test/snapshots.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import assert from "node:assert";
import { after, describe, test } from "node:test";
import { checkPackage, createPackageFromTarballData } from "@arethetypeswrong/core";

const updateSnapshots = process.env.UPDATE_SNAPSHOTS;
const updateSnapshots = process.env.UPDATE_SNAPSHOTS || process.env.U;
const testFilter = (process.env.TEST_FILTER || process.env.T)?.toLowerCase();

describe("snapshots", async () => {
const snapshotsWritten: URL[] = [];
Expand All @@ -29,9 +30,14 @@ describe("snapshots", async () => {
const errorPackages = ["Babel@0.0.1.tgz"];

for (const fixture of fs.readdirSync(new URL("../fixtures", import.meta.url))) {
if (fixture === ".DS_Store" || fixture.startsWith("@types__")) {
if (
fixture === ".DS_Store" ||
fixture.startsWith("@types__") ||
(testFilter && !fixture.toLowerCase().includes(testFilter))
) {
continue;
}

test(fixture, async () => {
const tarball = await readFile(new URL(`../fixtures/${fixture}`, import.meta.url));
const typesTarball = typesPackages[fixture]
Expand Down
Loading

0 comments on commit 57a08de

Please sign in to comment.