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

allow test cases to be written in conjure yml #90

Merged
merged 1 commit into from
Jun 4, 2019
Merged
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
"compile": "npm-run-all -p compile-ts compile-dist",
"compile-dist": "webpack --config webpack.config.js",
"compile-ts": "tsc -p ./src",
"generateTestCases": "./scripts/download-conjure-core.sh && ./scripts/generate-test-cases.sh",
"downloadTestServer": "./scripts/download-test-server.sh",
"generateSpecBindings": "./scripts/generate-test-bindings.sh",
"lint": "tslint 'src/**/*.ts*' --project ./src/tsconfig.json",
"lint-fix": "yarn lint -- --fix --project ./src/tsconfig.json",
"test": "yarn downloadTestServer && yarn generateSpecBindings && npm-run-all test:unit test:integration",
"test": "yarn downloadTestServer && yarn generateSpecBindings && yarn generateTestCases && npm-run-all test:unit test:integration",
"test:integration": "./scripts/run-integration-tests.sh",
"test:unit": "jest --config ./jest.config.js --rootDir ./",
"verify": "npm-run-all lint test"
Expand Down
13 changes: 13 additions & 0 deletions scripts/download-conjure-core.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

set -euo pipefail

VERSION="$(grep "^com.palantir.conjure:conjure" < versions.props | tail -1 | sed 's/^com.palantir.conjure:conjure = \(.*\)$/\1/')"
ARTIFACT_NAME="conjure-${VERSION}"
DOWNLOAD_OUTPUT="build/downloads/conjure.tgz"

mkdir -p build/downloads
curl -L "https://palantir.bintray.com/releases/com/palantir/conjure/conjure/${VERSION}/${ARTIFACT_NAME}.tgz" -o "$DOWNLOAD_OUTPUT"

tar xf "$DOWNLOAD_OUTPUT" -C build
mv "build/$ARTIFACT_NAME" build/conjure
14 changes: 14 additions & 0 deletions scripts/generate-test-cases.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash

set -euo pipefail

CONJURE=build/conjure/bin/conjure
TEST_CASE_DIR="build/ir-test-cases"

rm -rf ${TEST_CASE_DIR}
mkdir -p ${TEST_CASE_DIR}

for RELATIVE_PATH in src/commands/generate/__tests__/resources/definitions/*; do
FILE_NAME=${RELATIVE_PATH##*/}
${CONJURE} compile "$RELATIVE_PATH" "${TEST_CASE_DIR}/${FILE_NAME%.*}.json"
done
2 changes: 1 addition & 1 deletion src/commands/generate/__tests__/cliTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { createPackageJson, createTsconfigJson, GenerateCommand } from "../index

describe("generate command", () => {
let outDir: string;
const input = path.join(__dirname, "./resources/ir/service.json");
const input = path.join(__dirname, "../../../../build/ir-test-cases/example-types.json");
const generateCommand = new GenerateCommand();

beforeEach(() => {
Expand Down
69 changes: 67 additions & 2 deletions src/commands/generate/__tests__/generatorTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,23 @@
* limitations under the License.
*/

import { ITypeDefinition } from "conjure-api";
import * as fs from "fs";
import {
IAliasDefinition,
IConjureDefinition,
IEnumDefinition,
IObjectDefinition,
ITypeDefinition,
ITypeDefinitionVisitor,
ITypeName,
IUnionDefinition,
} from "conjure-api";
import * as fs from "fs-extra";
import * as path from "path";
import { directory } from "tempy";
import { loadConjureDefinition } from "../generateCommand";
import { generate } from "../generator";
import { typeNameToFilePath } from "../simpleAst";
import { assertOutputAndExpectedAreEqual } from "./testTypesGeneratorTest";

describe("generator", () => {
let outDir: string;
Expand Down Expand Up @@ -84,3 +96,56 @@ export { integrationSecond };
);
});
});

const irDir = path.join(__dirname, "../../../../build/ir-test-cases");
const testCaseDir = path.join(__dirname, "resources/test-cases");

describe("definitionTests", () => {
let tempDir: string;

beforeEach(() => {
tempDir = directory();
});

for (const fileName of fs.readdirSync(irDir)) {
it(`${fileName} produces equivalent TypeScript`, async () => {
const definitionFilePath = path.join(irDir, fileName);
const paths = fileName.substring(0, fileName.lastIndexOf("."));
const actualDir = path.join(testCaseDir, paths);
const outputDir = path.join(tempDir, paths);

await fs.mkdirp(outputDir);
const conjureDefinition = await loadConjureDefinition(definitionFilePath);

await generate(conjureDefinition, outputDir);

expectAllFilesAreTheSame(conjureDefinition, outputDir, actualDir);
});
}
});

function expectAllFilesAreTheSame(definition: IConjureDefinition, actualDir: string, expectedDir: string) {
for (const type of definition.types) {
// We do not currently generate anything for aliases
if (type.type === "alias") {
continue;
}
const relativeFilePath = typeNameToFilePath(ITypeDefinition.visit(type, typeNameVisitor));
assertOutputAndExpectedAreEqual(actualDir, expectedDir, relativeFilePath);
}

for (const service of definition.services) {
const relativeFilePath = typeNameToFilePath(service.serviceName);
assertOutputAndExpectedAreEqual(actualDir, expectedDir, relativeFilePath);
}
}

const typeNameVisitor: ITypeDefinitionVisitor<ITypeName> = {
alias: (p1: IAliasDefinition) => p1.typeName,
enum: (p1: IEnumDefinition) => p1.typeName,
object: (p1: IObjectDefinition) => p1.typeName,
union: (p1: IUnionDefinition) => p1.typeName,
unknown: (p1: ITypeDefinition) => {
throw new Error(`unknown type ${p1}`);
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
types:
definitions:
default-package: com.palantir.binary
objects:
BinaryExample:
fields:
binary: binary
OptionalExample:
fields:
item: optional<binary>
BinaryAliasExample:
alias: binary
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
types:
definitions:
default-package: com.palantir.product
errors:
InvalidTypeDefinition:
namespace: Conjure
code: INVALID_ARGUMENT
docs: Invalid Conjure type definition.
safe-args:
typeName: string
unsafe-args:
typeDef: any
InvalidServiceDefinition:
namespace: Conjure
code: INVALID_ARGUMENT
docs: Invalid Conjure service definition.
safe-args:
serviceName:
type: string
docs: Name of the invalid service definition.
unsafe-args:
serviceDef:
type: any
docs: Details of the invalid service definition.
JavaCompilationFailed:
namespace: ConjureJava
docs: Failed to compile Conjure definition to Java code.
code: INTERNAL
DifferentPackage:
package: com.palantir.another
namespace: Conjure
docs: Different package.
code: INTERNAL
Loading