Skip to content

Commit

Permalink
fixup! WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ezzatron committed Mar 30, 2024
1 parent 4aa1e1b commit ce3e1f5
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 3 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/.makefiles/
/artifacts/
/CHANGELOG.md
/test/fixture/specification/no-prettier/
10 changes: 7 additions & 3 deletions src/specification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ export async function render(variables: Variable<unknown>[]): Promise<string> {
async function prettyPrint(markdown: string): Promise<string> {
try {
const prettier = await import("prettier");
const options =
(await prettier.resolveConfig(join(process.cwd(), "ENVIRONMENT.md"))) ??
{};

const options = (await prettier.resolveConfig(
join(process.cwd(), "ENVIRONMENT.md"),
)) ?? {
printWidth: 80,
proseWrap: "always",
};

return (
await prettier.format(markdown, { ...options, parser: "markdown" })
Expand Down
35 changes: 35 additions & 0 deletions test/fixture/specification/no-prettier/prettier-not-configured.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Environment variables

The `<app>` app uses **declarative environment variables** powered by
**[Austenite]**.

[austenite]: https://github.com/ezzatron/austenite

| Name | Usage | Description |
| :-------------- | :------- | :-------------- |
| [`LOGO`](#logo) | Required | Main logo image |

<!-- prettier-ignore-start -->

> [!TIP]
> If you set an empty value for an environment variable, the app behaves as if that variable isn't set.
<!-- prettier-ignore-end -->

## `LOGO`

_Main logo image_

The `LOGO` variable is a **required** variable that takes **absolute URL**
values, or **relative URL** values relative to
`https://base.example.org/path/to/resource`.

### Example values

```sh
export LOGO=https://host.example.org/path/to/resource # URL (absolute)
```

```sh
export LOGO=path/to/resource # URL (relative)
```
32 changes: 32 additions & 0 deletions test/fixture/specification/no-prettier/prettier-unavailable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Environment variables

The `<app>` app uses **declarative environment variables** powered by **[Austenite]**.

[austenite]: https://github.com/ezzatron/austenite

| Name | Usage | Description |
| :-------------- | :------- | :-------------- |
| [`LOGO`](#logo) | Required | Main logo image |

<!-- prettier-ignore-start -->

> [!TIP]
> If you set an empty value for an environment variable, the app behaves as if that variable isn't set.
<!-- prettier-ignore-end -->

## `LOGO`

_Main logo image_

The `LOGO` variable is a **required** variable that takes **absolute URL** values, or **relative URL** values relative to `https://base.example.org/path/to/resource`.

### Example values

```sh
export LOGO=https://host.example.org/path/to/resource # URL (absolute)
```

```sh
export LOGO=path/to/resource # URL (relative)
```
100 changes: 100 additions & 0 deletions test/suite/specification-prettier.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { join } from "path";
import { fileURLToPath } from "url";
import {
afterAll,
beforeAll,
beforeEach,
describe,
expect,
it,
vi,
} from "vitest";
import { initialize, url } from "../../src/index.js";
import { MockConsole, createMockConsole } from "../helpers.js";

const fixturesPath = fileURLToPath(
new URL("../fixture/specification", import.meta.url),
);

describe("Specification documents (Prettier unavailable)", () => {
let exitCode: number | undefined;
let mockConsole: MockConsole;

beforeEach(() => {
exitCode = undefined;
vi.spyOn(process, "exit").mockImplementation((code) => {
exitCode = code ?? 0;

return undefined as never;
});

process.env = {
AUSTENITE_SPEC: "true",
};

mockConsole = createMockConsole();
});

describe("when Prettier is not installed", () => {
beforeAll(() => {
// eslint-disable-next-line @typescript-eslint/require-await
vi.doMock("prettier", async () => {
throw new Error('Cannot find module "prettier"');
});
});

afterAll(() => {
vi.doUnmock("prettier");
});

it("doesn't pretty print the specification output", async () => {
url("LOGO", "Main logo image", {
base: new URL("https://base.example.org/path/to/resource"),
});
await initialize();

await expect(mockConsole.readStdout()).toMatchFileSnapshot(
fixturePath("no-prettier/prettier-unavailable"),
);
expect(exitCode).toBe(0);
});
});

describe("when Prettier is installed but not configured", () => {
beforeAll(() => {
// eslint-disable-next-line @typescript-eslint/require-await
vi.doMock("prettier", async () => {
const prettier = await vi.importActual("prettier");

return {
...prettier,

// eslint-disable-next-line @typescript-eslint/require-await
async resolveConfig() {
return null;
},
};
});
});

afterAll(() => {
vi.doUnmock("prettier");
});

it("uses prose wrap at 80 columns", async () => {
url("LOGO", "Main logo image", {
base: new URL("https://base.example.org/path/to/resource"),
});
await initialize();

await expect(mockConsole.readStdout()).toMatchFileSnapshot(
fixturePath("no-prettier/prettier-not-configured"),
);
expect(exitCode).toBe(0);
});
});
});

function fixturePath(name: string): string {
return join(fixturesPath, `${name}.md`);
}

0 comments on commit ce3e1f5

Please sign in to comment.