Skip to content

Commit

Permalink
Merge pull request #7 from pkl-community/jg-e2e-first
Browse files Browse the repository at this point in the history
Add first e2e test
  • Loading branch information
jasongwartz authored Mar 2, 2024
2 parents 1272520 + df7855f commit d2e24f6
Show file tree
Hide file tree
Showing 21 changed files with 4,503 additions and 1,197 deletions.
40 changes: 0 additions & 40 deletions .github/workflows/test-codegen.yaml

This file was deleted.

77 changes: 77 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: test

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
pkl-unit:
runs-on: ubuntu-latest

strategy:
matrix:
pklVersion: ["0.25.0", "0.25.1"]

steps:
- uses: actions/checkout@v2

# Install pkl binary
- name: Create /tmp/bin and add to PATH
run: |
mkdir -p "{{ runner.temp }}/bin" && \
echo "{{ runner.temp }}/bin" >> "$GITHUB_PATH"
- name: Fetch Pkl binary from GitHub release
uses: dsaltares/fetch-gh-release-asset@master
with:
repo: apple/pkl
version: tags/${{ matrix.pklVersion }}
file: pkl-linux-amd64
target: "{{ runner.temp }}/bin/pkl"
token: ${{ secrets.GITHUB_TOKEN }}

- name: Make pkl binary executable
run: chmod +x "{{ runner.temp }}/bin/pkl"

- name: Run tests
run: |
tests=$(find . -name '*.test.pkl')
pkl test $tests
e2e:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18, 20]
pklVersion: ["0.25.0", "0.25.1"]

steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: npm

# Install pkl binary
- name: Create /tmp/bin and add to PATH
run: |
mkdir -p "{{ runner.temp }}/bin" && \
echo "{{ runner.temp }}/bin" >> "$GITHUB_PATH"
- name: Fetch Pkl binary from GitHub release
uses: dsaltares/fetch-gh-release-asset@master
with:
repo: apple/pkl
version: tags/${{ matrix.pklVersion }}
file: pkl-linux-amd64
target: "{{ runner.temp }}/bin/pkl"
token: ${{ secrets.GITHUB_TOKEN }}

- name: Make pkl binary executable
run: chmod +x "{{ runner.temp }}/bin/pkl"

- run: npm install
- run: npm run test:e2e
2 changes: 1 addition & 1 deletion bin/pkl-gen-typescript.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env tsx
import pklGenTypescript from "../pkl-gen-typescript/main";

pklGenTypescript();
pklGenTypescript(process.argv.slice(2));
2 changes: 1 addition & 1 deletion codegen/snippet-tests/output/with_class.pkl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface WithClass {
value: MyCustomClass
}

// Ref: Pkl class `MyCustomClass`.
// Ref: Pkl class `withClass.MyCustomClass`.
export interface MyCustomClass {
x: string

Expand Down
2 changes: 1 addition & 1 deletion codegen/src/Generator.pkl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// Generates TypeScript sources from Pkl
@typescript.Module { name = "pkl_gen_typescript" }
@ModuleInfo { minPklVersion = "0.25.1" }
@ModuleInfo { minPklVersion = "0.25.0" }
module pkl.typescript.Generator

import "pkl:reflect"
Expand Down
2 changes: 0 additions & 2 deletions codegen/src/GeneratorSettings.pkl
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// Settings used to configure code generation.
@typescript.Module { name = "pkl_gen_typescript" }
@typescript.Name { value = "GeneratorSettings" }
module pkl.typescript.GeneratorSettings

import "pkl:reflect"
Expand Down
7 changes: 6 additions & 1 deletion codegen/src/internal/TypescriptModule.pkl
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ contents: String = new Listing {
// LoadFromPath loads the pkl module at the given path and evaluates it into a \(moduleClass.name)
export const loadFromPath = async (path: string): Promise<\(moduleClass.type.render(moduleClass.typescriptModule))> => {
const evaluator = await pklTypescript.newEvaluator(pklTypescript.PreconfiguredOptions);
return load(evaluator, pklTypescript.FileSource(path));
try {
const result = await load(evaluator, pklTypescript.FileSource(path));
return result
} finally {
evaluator.close()
}
};
export const load = (evaluator: pklTypescript.Evaluator, source: pklTypescript.ModuleSource): Promise<\(moduleClass.type.render(moduleClass.typescriptModule))> =>
Expand Down
4 changes: 4 additions & 0 deletions e2e/only_primitives/correct.pkl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
amends "schema.pkl"

addr = "localhost"
port = 3000
3 changes: 3 additions & 0 deletions e2e/only_primitives/missingRequired.pkl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
amends "schema.pkl"

addr = "localhost"
4 changes: 4 additions & 0 deletions e2e/only_primitives/outOfBounds.pkl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
amends "schema.pkl"

addr = "localhost"
port = 3000000000000
33 changes: 33 additions & 0 deletions e2e/only_primitives/primitives.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { describe, expect, it } from "@jest/globals";

import pklGenTypescript from "../../pkl-gen-typescript/main";
import { join } from "path";

describe("E2E of config with only primitive values", () => {
it("can generate TypeScript sources and load valid values", async () => {
await pklGenTypescript([join(__dirname, "schema.pkl")]);
const configPkl = await import(join(__dirname, "../../.out/schema.pkl.ts"));
const config = await configPkl.loadFromPath(join(__dirname, "correct.pkl"));
expect(config).toStrictEqual({
addr: "localhost",
port: 3000,
});
});

it.each([
["missing a required property", "missingRequired.pkl"],
["property is of the wrong type", "wrongType.pkl"],
["property fails validation constraint", "outOfBounds.pkl"],
])(
"can generate TypeScript sources but error on evaluating invalid values: %s",
async (_, fileBase) => {
await pklGenTypescript([join(__dirname, "schema.pkl")]);
const configPkl = await import(
join(__dirname, "../../.out/schema.pkl.ts")
);
await expect(
configPkl.loadFromPath(join(__dirname, `${fileBase}.pkl`))
).rejects.toThrowError();
}
);
});
2 changes: 2 additions & 0 deletions e2e/only_primitives/schema.pkl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
addr: String
port: Int16
4 changes: 4 additions & 0 deletions e2e/only_primitives/wrongType.pkl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
amends "schema.pkl"

addr = "localhost"
port = "3000"
8 changes: 8 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
moduleNameMapper: {
"^@pkl-community/pkl-typescript$": "<rootDir>/src",
},
};
Loading

0 comments on commit d2e24f6

Please sign in to comment.