Skip to content

Commit

Permalink
test: make tests run on Windows (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
wkillerud authored Aug 16, 2024
1 parent 7fb7556 commit ab055c5
Showing 1 changed file with 75 additions and 19 deletions.
94 changes: 75 additions & 19 deletions tests/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { Writable, pipeline } from "node:stream";
import { URL } from "node:url";
import { fileURLToPath } from "node:url";
import { stream } from "@eik/common";
import tap from "tap";
import slug from "unique-slug";
Expand All @@ -14,12 +14,16 @@ tap.cleanSnapshot = (s) => {
return s.replace(regex, '"timestamp": -1,');
};

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const DEFAULT_CONFIG = {
sinkFsRootPath: path.join(os.tmpdir(), "/eik-test-files"),
sinkFsRootPath: path.join(os.tmpdir(), "eik-test-files"),
};
const FIXTURE = fs
.readFileSync(new URL("../fixtures/import-map.json", import.meta.url))
.toString();
const FIXTURE = fs.readFileSync(
path.join(__dirname, "..", "fixtures", "import-map.json"),
"utf-8",
);

const MetricsInto = class MetricsInto extends Writable {
constructor() {
Expand All @@ -43,7 +47,7 @@ const MetricsInto = class MetricsInto extends Writable {
};

const readFileStream = (file = "../README.md") =>
fs.createReadStream(new URL(file, import.meta.url));
fs.createReadStream(path.join(__dirname, file));

const pipeInto = (...streams) =>
new Promise((resolve, reject) => {
Expand Down Expand Up @@ -94,7 +98,11 @@ tap.test("Sink() - .write()", async (t) => {
t.resolves(pipe(writeFrom, writeTo), "should write file to sink");

// Clean up sink
await sink.delete(dir);
try {
await sink.delete(dir);
} catch {
/* do nothing*/
}
t.end();
});

Expand Down Expand Up @@ -153,7 +161,11 @@ tap.test("Sink() - .write() - directory traversal prevention", async (t) => {
);

// Clean up sink
await sink.delete(dir);
try {
await sink.delete(dir);
} catch {
/* do nothing*/
}
t.end();
});

Expand Down Expand Up @@ -193,7 +205,11 @@ tap.test("Sink() - .read() - File exists", async (t) => {
);

// Clean up sink
await sink.delete(dir);
try {
await sink.delete(dir);
} catch {
/* do nothing*/
}
t.end();
});

Expand Down Expand Up @@ -254,7 +270,11 @@ tap.test("Sink() - .read() - directory traversal prevention", async (t) => {
);

// Clean up sink
await sink.delete(dir);
try {
await sink.delete(dir);
} catch {
/* do nothing*/
}
t.end();
});

Expand All @@ -279,7 +299,11 @@ tap.test(
);

// Clean up sink
await sink.delete(dir);
try {
await sink.delete(dir);
} catch {
/* do nothing*/
}
t.end();
},
);
Expand All @@ -305,7 +329,11 @@ tap.test(
);

// Clean up sink
await sink.delete(dir);
try {
await sink.delete(dir);
} catch {
/* do nothing*/
}
t.end();
},
);
Expand All @@ -331,7 +359,11 @@ tap.test("Sink() - .delete() - Delete existing file", async (t) => {
t.rejects(sink.exist(file), "should reject - file was deleted");

// Clean up sink
await sink.delete(dir);
try {
await sink.delete(dir);
} catch {
/* do nothing*/
}
t.end();
});

Expand Down Expand Up @@ -364,7 +396,11 @@ tap.test("Sink() - .delete() - Delete file in tree structure", async (t) => {
);

// Clean up sink
await sink.delete(dir);
try {
await sink.delete(dir);
} catch {
/* do nothing*/
}
t.end();
});

Expand All @@ -382,7 +418,11 @@ tap.test("Sink() - .delete() - Delete files recursively", async (t) => {
const writeToB = await sink.write(fileB, "application/json");
await pipe(writeFromB, writeToB);

await sink.delete(dir);
try {
await sink.delete(dir);
} catch {
/* do nothing*/
}

t.rejects(sink.exist(fileA), "should reject on file A - file was deleted");
t.rejects(sink.exist(fileB), "should reject on file B - file was deleted");
Expand Down Expand Up @@ -435,7 +475,11 @@ tap.test("Sink() - .delete() - directory traversal prevention", async (t) => {
);

// Clean up sink
await sink.delete(dir);
try {
await sink.delete(dir);
} catch {
/* do nothing*/
}
t.end();
});

Expand All @@ -452,7 +496,11 @@ tap.test("Sink() - .exist() - Check existing file", async (t) => {
t.resolves(sink.exist(file), "should resolve - file is in sink");

// Clean up sink
await sink.delete(dir);
try {
await sink.delete(dir);
} catch {
/* do nothing*/
}
t.end();
});

Expand Down Expand Up @@ -515,7 +563,11 @@ tap.test("Sink() - .exist() - directory traversal prevention", async (t) => {
);

// Clean up sink
await sink.delete(dir);
try {
await sink.delete(dir);
} catch {
/* do nothing*/
}
t.end();
});

Expand All @@ -537,7 +589,11 @@ tap.test("Sink() - .metrics - all successfull operations", async (t) => {
const readFrom = await sink.read(file);
await pipeInto(readFrom.stream);

await sink.delete(dir);
try {
await sink.delete(dir);
} catch {
/* do nothing*/
}

const metrics = await metricsInto.done();
t.matchSnapshot(metrics, "metrics should match snapshot");
Expand Down

0 comments on commit ab055c5

Please sign in to comment.