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

update Node.js harness #178

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions validator/imported/.babelrc

This file was deleted.

7 changes: 7 additions & 0 deletions validator/imported/babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"plugins": ["@babel/plugin-transform-typescript"],
"presets": [
"@babel/preset-env",
"@babel/preset-flow"
]
}
228 changes: 120 additions & 108 deletions validator/imported/export.js
Original file line number Diff line number Diff line change
@@ -1,150 +1,162 @@
import fs from 'fs';
import Module from 'module';
import { testSchema } from './graphql-js/src/validation/__tests__/harness';
import { printSchema } from './graphql-js/src/utilities';
import yaml from 'js-yaml';
import fs from "fs";
import Module from "module";
import { testSchema } from "./graphql-js/src/validation/__tests__/harness";
import { printSchema } from "./graphql-js/src/utilities";
import yaml from "js-yaml";

let schemas = [];
function registerSchema(schema) {
for (let i = 0; i < schemas.length; i++) {
if (schemas[i] === schema) {
return i;
}
for (let i = 0; i < schemas.length; i++) {
if (schemas[i] === schema) {
return i;
}
schemas.push(schema);
return schemas.length - 1;
}
schemas.push(schema);
return schemas.length - 1;
}

function resultProxy(start) {
let handler = {
get: function(obj, prop) {
if (typeof prop === 'symbol') {
console.log("RET");
return obj
}
return new Proxy({path: obj.path + "." + prop}, handler)
},
};
function resultProxy(start, base = {}) {
const funcWithPath = (path) => {
const f = () => {};
f.path = path;
return f;
};
let handler = {
get: function (obj, prop) {
if (base[prop]) {
return base[prop];
}
return new Proxy(funcWithPath(`${obj.path}.${prop}`), handler);
},
};

return new Proxy({path:start}, handler);
return new Proxy(funcWithPath(start), handler);
}

// replace empty lines with the normal amount of whitespace
// so that yaml correctly preserves the whitespace
function normalizeWs(rawString) {
const lines = rawString.split(/\r\n|[\n\r]/g);
const lines = rawString.split(/\r\n|[\n\r]/g);

let commonIndent = 1000000;
for (let i = 1; i < lines.length; i++) {
const line = lines[i];
if (line.trim() === '') continue;
let commonIndent = 1000000;
for (let i = 1; i < lines.length; i++) {
const line = lines[i];
if (!line.trim()) {
continue;
}

const indent = line.search(/\S/);
if (indent < commonIndent) {
commonIndent = indent;
}
const indent = line.search(/\S/);
if (indent < commonIndent) {
commonIndent = indent;
}
}

for (let i = 0; i < lines.length; i++) {
if (lines[i].length < commonIndent) {
lines[i] = ' '.repeat(commonIndent);
}
for (let i = 0; i < lines.length; i++) {
if (lines[i].length < commonIndent) {
lines[i] = " ".repeat(commonIndent);
}
return lines.join('\n');
}
return lines.join("\n");
}

const harness = {
expectPassesRule(rule, queryString) {
harness.expectPassesRuleWithSchema(testSchema, rule, queryString);
},
expectPassesRuleWithSchema(schema, rule, queryString, errors) {
tests.push({
name: names.slice(1).join('/'),
rule: rule.name,
schema: registerSchema(schema),
query: normalizeWs(queryString),
errors: [],
});
},
expectFailsRule(rule, queryString, errors) {
harness.expectFailsRuleWithSchema(testSchema, rule, queryString, errors);
testSchema,

return resultProxy("errors")
},
expectFailsRuleWithSchema(schema, rule, queryString, errors) {
expectValidationErrorsWithSchema(schema, rule, queryStr) {
return resultProxy("expectValidationErrorsWithSchema", {
toDeepEqual(expected) {
tests.push({
name: names.slice(1).join('/'),
rule: rule.name,
schema: registerSchema(schema),
query: normalizeWs(queryString),
errors: errors,
name: names.slice(1).join("/"),
rule: rule.name,
schema: registerSchema(schema),
query: normalizeWs(queryStr),
errors: expected,
});
}
},
});
},
expectValidationErrors(rule, queryStr) {
return harness.expectValidationErrorsWithSchema(testSchema, rule, queryStr);
},
expectSDLValidationErrors(schema, rule, sdlStr) {
return resultProxy("expectSDLValidationErrors", {
toDeepEqual(expected) {
// ignore now...
// console.warn(rule.name, sdlStr, JSON.stringify(expected, null, 2));
},
});
},
};


let tests = [];
let names = [];
const fakeModules = {
'mocha': {
describe(name, f) {
names.push(name);
f();
names.pop();
mocha: {
describe(name, f) {
names.push(name);
f();
names.pop();
},
it(name, f) {
names.push(name);
f();
names.pop();
},
},
chai: {
expect(it) {
const expect = {
get to() {
return expect;
},
it(name, f) {
names.push(name);
f();
names.pop();
get have() {
return expect;
},

},
'chai': {
expect(it) {
const expect = {
get to() {
return expect
},
equal(value) {
// currently ignored, we know all we need to add an assertion here.
},
};

return expect
get nested() {
return expect;
},
equal(value) {
// currently ignored, we know all we need to add an assertion here.
},
property(path, value) {
// currently ignored, we know all we need to add an assertion here.
},
};

return expect;
},
'./harness': harness,
},
"./harness": harness,
};

const originalLoader = Module._load;
Module._load = function(request, parent, isMain) {
return fakeModules[request] || originalLoader(request, parent, isMain);
Module._load = function (request, parent, isMain) {
return fakeModules[request] || originalLoader(request, parent, isMain);
};

fs.readdirSync("./graphql-js/src/validation/__tests__").forEach(file => {
if (!file.endsWith('-test.js')) {
return
}
fs.readdirSync("./graphql-js/src/validation/__tests__").forEach((file) => {
if (!file.endsWith("-test.ts")) {
return;
}

if (file === 'validation-test.js') {
return
}
if (file === "validation-test.ts") {
return;
}

require('./graphql-js/src/validation/__tests__/' + file);
require(`./graphql-js/src/validation/__tests__/${file}`);

let dump = yaml.dump(tests, {
skipInvalid: true,
flowLevel: 5,
noRefs: true,
lineWidth: 1000,
});
fs.writeFileSync("./spec/"+file.replace('-test.js', '.spec.yml'), dump);
let dump = yaml.dump(tests, {
skipInvalid: true,
flowLevel: 5,
noRefs: true,
lineWidth: 1000,
});
fs.writeFileSync(`./spec/${file.replace("-test.ts", ".spec.yml")}`, dump);

tests = [];
tests = [];
});

let schemaList = schemas.map(s => printSchema(s));

let schemaList = schemas.map((s) => printSchema(s));

schemaList[0] += `
# injected becuase upstream spec is missing some types
Expand All @@ -171,9 +183,9 @@ type T {
}`;

let dump = yaml.dump(schemaList, {
skipInvalid: true,
flowLevel: 5,
noRefs: true,
lineWidth: 1000,
skipInvalid: true,
flowLevel: 5,
noRefs: true,
lineWidth: 1000,
});
fs.writeFileSync("./spec/schemas.yml", dump);
4 changes: 2 additions & 2 deletions validator/imported/export.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ EXPORTER_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

cd "$EXPORTER_ROOT" || exit

GIT_REF=origin/master
GIT_REF=origin/main

if [[ -f "$EXPORTER_ROOT/graphql-js-commit.log" ]] ; then
GIT_REF=$(cat "$EXPORTER_ROOT/graphql-js-commit.log")
Expand All @@ -32,4 +32,4 @@ echo "installing js dependencies"
npm ci

echo "exporting tests"
npx babel-node ./export.js
npx babel-node -x ".ts,.js" ./export.js
2 changes: 1 addition & 1 deletion validator/imported/graphql-js-commit.log
Original file line number Diff line number Diff line change
@@ -1 +1 @@
e6c36e0725ea0aabad1a19c68e54180fb7092e3a
f597c694339b7c488f05496806e404659f6ff955
Loading