Feature/jest2 (#322)

* Adding more tests
* adding jest
* fixing tests
* fixing linting
* removing 1 slash less
* fixing more tests
* fixing last test
* more last fixes
This commit is contained in:
Daan Verstraten
2024-11-09 10:08:30 +01:00
committed by GitHub
parent b788f118ff
commit 8e59f63167
39 changed files with 8224 additions and 1158 deletions

View File

@@ -1,45 +1,23 @@
import { expect } from "chai";
import { Github } from "../github";
import { Schema } from "../schema-tester";
import { Files } from "../utillity";
describe("test correct files", function () {
const folder = Files.CorrectFilesFolder().replace(/\\/gi, "/");
const files = Files.GetFiles(folder);
const files = Files.GetFiles(folder).filter((f) => f.endsWith(".json"));
const validator = Schema.GetValidator();
expect(files.length, "No files were returned").to.greaterThan(0);
expect(files.length).toBeGreaterThan(0);
files
.filter((f) => f.endsWith(".json"))
.forEach((file) => {
const testfolder = file.replace(folder + "/", "");
test.each(files)("File should have a schema & validate correctly: %s", async (file) => {
const result = validator.ValidateFile(file);
const schemas = validator.ls.getMatchingSchemas(result.doc, result.jdoc);
it(`File should have a schema & validate correctly: ${testfolder}`, async function () {
const result = validator.ValidateFile(file);
const schemas = validator.ls.getMatchingSchemas(result.doc, result.jdoc);
const succes = await result.promise;
result.promise.then(
(succes) => {
expect(succes.length, "Expected no errors got: " + succes.length).to.equal(0);
succes.forEach((item) => console.log(item.message));
},
(fail) => {
Github.createError("Failed on validating", { file: file });
expect.fail("Failed to validate");
}
);
schemas.then(
(success) => {
expect(success.length, "Expected schemas to be returned").to.greaterThan(0);
},
(fail) => {
Github.createError("Failed on retrieving schemas", { file: file });
expect.fail("failed on retrieving schemas");
}
);
expect(succes).toHaveLength(0);
succes.forEach((item) => console.log(item.message));
return Promise.all([result.promise, schemas]);
});
});
const s = await schemas;
expect(s.length).toBeGreaterThan(0);
});
});

View File

@@ -1,44 +1,20 @@
import { expect } from "chai";
import { Github } from "../github";
import { Schema } from "../schema-tester";
import { Files } from "../utillity";
describe("test incorrect files", function () {
const folder = Files.InCorrectFilesFolder().replace(/\\/gi, "/");
const files = Files.GetFiles(folder);
const files = Files.GetFiles(folder).filter((f) => f.endsWith(".json"));
const validator = Schema.GetValidator();
expect(files.length, "No files were returned").to.greaterThan(0);
expect(files.length).toBeGreaterThan(0);
files
.filter((f) => f.endsWith(".json"))
.forEach((file) => {
const testfolder = file.replace(folder + "/", "");
test.each(files)("File should invalidate & have a schema: %s", async (file) => {
const result = validator.ValidateFile(file);
const schemas = validator.ls.getMatchingSchemas(result.doc, result.jdoc);
it(`File should invalidate & have a schema: ${testfolder}`, async function () {
const result = validator.ValidateFile(file);
const schemas = validator.ls.getMatchingSchemas(result.doc, result.jdoc);
result.promise.then(
(succes) => {
expect(succes.length, "Expected errors! but had none").to.greaterThan(0);
},
() => {
Github.createError("No errors where found", { file: file });
expect.fail("Failed to validate");
}
);
schemas.then(
(success) => {
expect(success.length, "Expected schemas to be returned").to.greaterThan(0);
},
() => {
Github.createError("Found no schema", { file: file });
expect.fail("failed on retrieving schemas");
}
);
return Promise.all([schemas, result]);
});
});
const success = await result.promise;
expect(success.length).toBeGreaterThan(0);
const s = await schemas;
expect(s.length).toBeGreaterThan(0);
});
});

View File

@@ -9,7 +9,7 @@ export interface ErrorAnnotation {
export namespace Github {
export function createError(message: string, error: ErrorAnnotation = {}): void {
const data = Object.entries(error)
.filter(([key, value]) => value !== undefined)
.filter(([, value]) => value !== undefined)
.map(([key, value]) => `${key}=${value}`)
.join(",");

View File

@@ -1,7 +1,8 @@
import { getLanguageService, LanguageService, LanguageSettings, SchemaConfiguration, TextDocument, JSONDocument, Diagnostic, Thenable } from "vscode-json-languageservice";
import { existsSync, readFileSync } from "fs";
import * as path from "path";
import * as url from "url";
import { Diagnostic, getLanguageService, JSONDocument, LanguageService, LanguageSettings, SchemaConfiguration, TextDocument, Thenable } from "vscode-json-languageservice";
import * as data from "../../vscode-settings.json";
import { readFileSync } from "fs";
import { Files } from "./utillity";
export namespace Schema {
@@ -21,26 +22,41 @@ export namespace Schema {
}
export function GetLanguageService(): LanguageService {
return getLanguageService({ workspaceContext });
return getLanguageService({
workspaceContext,
schemaRequestService: getSchema,
});
}
export async function getSchema(uri: string): Promise<string> {
const rootfolder = Files.RootFolder();
const filepath = uri.replace("https://raw.githubusercontent.com/Blockception/Minecraft-bedrock-json-schemas/main", rootfolder);
if (!existsSync(filepath)) {
throw new Error("file doesn't exist: " + filepath);
}
return readFileSync(filepath).toString();
}
export function GetLanguageSettings(): LanguageSettings {
const schemas: SchemaConfiguration[] = [];
const settings: LanguageSettings = { schemas: schemas };
const settings: LanguageSettings = {
schemas: schemas,
};
let rootfolder = Files.RootFolder();
if (!rootfolder.endsWith("/")) rootfolder += "/";
rootfolder = path.normalize(rootfolder);
data["json.schemas"].forEach((m) => {
if (m) {
const schema = m.url.replace("https://raw.githubusercontent.com/Blockception/Minecraft-bedrock-json-schemas/main/", rootfolder);
let matches = m.fileMatch;
if (typeof matches === "string") {
matches = [matches];
}
schemas.push({ uri: schema, fileMatch: matches });
schemas.push({ uri: m.url, fileMatch: matches });
}
});

View File

@@ -1,19 +1,17 @@
import { expect } from "chai";
import { DummyFiles } from "../../src/main";
import { Files } from "./utillity";
describe("Files", function () {
it("Root", function () {
const temp = Files.RootFolder();
expect(temp.endsWith("lib"), "ended with lib").to.be.false;
expect(temp.endsWith("lib\\test"), "ended with lib\\test").to.be.false;
expect(temp.endsWith("lib/test"), "ended with lib/test").to.be.false;
expect(temp.endsWith("lib")).toBeFalsy();
expect(temp.endsWith("lib\\test")).toBeFalsy();
expect(temp.endsWith("lib/test")).toBeFalsy();
});
it("Test", function () {
const temp = Files.TestFolder();
expect(temp.endsWith("lib"), "ended with lib").to.be.false;
expect(temp.endsWith("lib")).toBeFalsy();
});
});

View File

@@ -1,5 +1,5 @@
import FastGlob = require("fast-glob");
import path = require("path");
import * as FastGlob from "fast-glob";
import * as path from "path";
export namespace Files {
export function TestFolder(): string {

View File

@@ -1,35 +1,24 @@
import path = require("path");
import { Files } from "./utillity";
import * as fs from "fs";
import * as JSONC from "comment-json";
import { expect } from "chai";
import * as fs from "fs";
import * as path from "path";
import { ErrorAnnotation, Github } from "./github";
import { Files } from "./utillity";
describe("Validate", function () {
const folder = path.join(Files.TestFolder(), "..", "source");
console.log(folder);
const files = Files.GetFiles(folder);
expect(files.length).toBeGreaterThan(0);
files.forEach((filepath) => {
const filename = filepath.slice(folder.length);
test.each(files)("Validating schema parts: %s", (filepath) => {
const data = fs.readFileSync(filepath, "utf8");
const object = JSONC.parse(data) as JsonSchema;
expect(object).toBeDefined();
if (!object) {
return;
}
it(`Validating schema parts: ${filename}`, function () {
let object: JsonSchema | undefined = undefined;
let data: string;
data = fs.readFileSync(filepath, "utf8");
object = <JsonSchema>JSONC.parse(data);
expect(object).to.not.be.undefined;
expect(object).to.not.be.null;
if (!object) {
this.skip();
return;
}
const explorer = new Explorer(data, filepath);
explorer.explore_refs(object, path.dirname(filepath));
});
const explorer = new Explorer(data, filepath);
explorer.explore_refs(object, path.dirname(filepath));
});
});
@@ -60,7 +49,7 @@ class Explorer {
anno.file = this.filepath;
Github.createError(`Ref not found: ${ref}`, anno);
expect.fail(`ref ${ref} does not exists`);
throw new Error(`ref ${ref} does not exists`);
}
}
}