Skip to content

Commit a1baa11

Browse files
authored
test: add more test cases (#444)
1 parent 668825c commit a1baa11

File tree

12 files changed

+154
-106
lines changed

12 files changed

+154
-106
lines changed

packages/autocorrect/test/__snapshots__/fixtures.spec.ts.snap

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3-
exports[`parser and printer > should format all fixtures > test.css 1`] = `
3+
exports[`parser and printer > should format test.css fixture 1`] = `
44
"/*
55
这是多行 CSS 第 1 行
66
这是第 2 行
@@ -14,7 +14,7 @@ exports[`parser and printer > should format all fixtures > test.css 1`] = `
1414
"
1515
`;
1616

17-
exports[`parser and printer > should format all fixtures > test.go 1`] = `
17+
exports[`parser and printer > should format test.go fixture 1`] = `
1818
"// WithContext 创建基于 ctx 的 db
1919
// 第 2 行注释
2020
func (d *Dao) WithContext(ctx context.Context) (db *gorm.DB) {
@@ -34,7 +34,7 @@ func (d *Dao) WithContext(ctx context.Context) (db *gorm.DB) {
3434
"
3535
`;
3636
37-
exports[`parser and printer > should format all fixtures > test.html 1`] = `
37+
exports[`parser and printer > should format test.html fixture 1`] = `
3838
"<h1>编译 rust 为 WebAssembly</h1>
3939
<div></div>
4040
<p class="summary">如果你写了一些 RUST 代码,你可以把它编译成 webAssembly!这份教程将带你编译 Rust 项目为 wasm 并在一个现存的 web 应用中使用它。</p>
@@ -49,7 +49,7 @@ exports[`parser and printer > should format all fixtures > test.html 1`] = `
4949
"
5050
`;
5151
52-
exports[`parser and printer > should format all fixtures > test.md 1`] = `
52+
exports[`parser and printer > should format test.md fixture 1`] = `
5353
"# 这是 Heading 1 大标题
5454
5555
**加粗**
@@ -102,7 +102,7 @@ let a = "你好 hello";
102102
"
103103
`;
104104
105-
exports[`parser and printer > should format all fixtures > test.py 1`] = `
105+
exports[`parser and printer > should format test.py fixture 1`] = `
106106
"'''
107107
这是多行 1 注释
108108
这是多行 2 注释
@@ -125,7 +125,7 @@ def hello(a):
125125
"
126126
`;
127127
128-
exports[`parser and printer > should format all fixtures > test.rb 1`] = `
128+
exports[`parser and printer > should format test.rb fixture 1`] = `
129129
"# 第 1 行注释
130130
# 第 2 行注释
131131
def hello(a, b: "第 1 个参数")
@@ -139,7 +139,7 @@ end
139139
"
140140
`;
141141
142-
exports[`parser and printer > should format all fixtures > test0.js 1`] = `
142+
exports[`parser and printer > should format test0.js fixture 1`] = `
143143
"/* eslint-disable camelcase, no-unused-vars, sonarjs/no-dead-store, sonarjs/no-unused-vars */
144144
/**
145145
* Hello 你好

packages/autocorrect/test/fixtures.spec.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,24 @@ const _dirname =
1212
: __dirname
1313

1414
describe('parser and printer', () => {
15-
it('should format all fixtures', async () => {
16-
const fixtures = path.resolve(_dirname, 'fixtures')
17-
for (const relativeFilepath of fs.readdirSync(fixtures)) {
18-
const filepath = path.resolve(fixtures, relativeFilepath)
19-
const input = fs.readFileSync(filepath, 'utf8')
15+
const fixtures = path.resolve(_dirname, 'fixtures')
2016

17+
for (const relativeFilepath of fs.readdirSync(fixtures)) {
18+
const filepath = path.resolve(fixtures, relativeFilepath)
19+
const input = fs.readFileSync(filepath, 'utf8')
20+
21+
it(`should format ${relativeFilepath} fixture`, async () => {
2122
try {
2223
const output = await format(input, {
2324
filepath,
2425
parser: 'autocorrect',
2526
plugins: [AutocorrectPlugin],
2627
})
2728

28-
expect(output).toMatchSnapshot(relativeFilepath)
29+
expect(output).toMatchSnapshot()
2930
} catch (err: unknown) {
30-
expect((err as Error).message).toMatchSnapshot(relativeFilepath)
31+
expect((err as Error).message).toMatchSnapshot()
3132
}
32-
}
33-
})
33+
})
34+
}
3435
})

packages/sh/test/__snapshots__/fixtures.spec.ts.snap

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3-
exports[`parser and printer > should format .dockerignore fixtures > .dockerignore 1`] = `
3+
exports[`parser and printer > should format .dockerignore fixture correctly 1`] = `
44
"*.less.json
55
*.log
66
*.tsbuildinfo
@@ -11,12 +11,12 @@ node_modules
1111
"
1212
`;
1313

14-
exports[`parser and printer > should format .nvmrc fixtures > .nvmrc 1`] = `
14+
exports[`parser and printer > should format .nvmrc fixture correctly 1`] = `
1515
"v1.2.3
1616
"
1717
`;
1818

19-
exports[`parser and printer > should format .properties fixtures > .properties 1`] = `
19+
exports[`parser and printer > should format .properties fixture correctly 1`] = `
2020
"# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2121
# yarn lockfile v1
2222
@@ -26,7 +26,7 @@ username jounqin
2626
"
2727
`;
2828

29-
exports[`parser and printer > should format 133.sh fixtures > 133.sh 1`] = `
29+
exports[`parser and printer > should format 133.sh fixture correctly 1`] = `
3030
"#!/usr/bin/env bash
3131
3232
set -euo pipefail
@@ -218,7 +218,7 @@ main "$@"
218218
"
219219
`;
220220
221-
exports[`parser and printer > should format 162.sh fixtures > 162.sh 1`] = `
221+
exports[`parser and printer > should format 162.sh fixture correctly 1`] = `
222222
"#!/usr/bin/env bash
223223
224224
{ # this ensures the entire script is downloaded #
@@ -700,7 +700,7 @@ exports[`parser and printer > should format 162.sh fixtures > 162.sh 1`] = `
700700
"
701701
`;
702702
703-
exports[`parser and printer > should format 182.sh fixtures > 182.sh 1`] = `
703+
exports[`parser and printer > should format 182.sh fixture correctly 1`] = `
704704
"#!/usr/bin/env bash
705705
set -euo pipefail
706706
@@ -756,7 +756,7 @@ fi
756756
"
757757
`;
758758
759-
exports[`parser and printer > should format 191.sh fixtures > 191.sh 1`] = `
759+
exports[`parser and printer > should format 191.sh fixture correctly 1`] = `
760760
"#!/bin/sh
761761
762762
set -eu
@@ -836,20 +836,20 @@ esac
836836
"
837837
`;
838838
839-
exports[`parser and printer > should format 278.Dockerfile fixtures > 278.Dockerfile 1`] = `
839+
exports[`parser and printer > should format 278.Dockerfile fixture correctly 1`] = `
840840
"FROM ubuntu
841841
842842
RUN (cd out && ls)
843843
"
844844
`;
845845
846-
exports[`parser and printer > should format 292.Dockerfile fixtures > 292.Dockerfile 1`] = `
846+
exports[`parser and printer > should format 292.Dockerfile fixture correctly 1`] = `
847847
"FROM node:20.9.0-alpine
848848
RUN if [[ x$LATEST_NPM = xtrue ]]; then yarn global add npm@latest; fi
849849
"
850850
`;
851851
852-
exports[`parser and printer > should format 376.Dockerfile fixtures > 376.Dockerfile 1`] = `
852+
exports[`parser and printer > should format 376.Dockerfile fixture correctly 1`] = `
853853
"FROM debian:12.6-slim
854854
855855
RUN set -eux; for x in {1..3}; do echo 'foo'; echo 'bar'; echo "$x"; done
@@ -865,12 +865,12 @@ EOF
865865
"
866866
`;
867867
868-
exports[`parser and printer > should format 384.Dockerfile fixtures > 384.Dockerfile 1`] = `
868+
exports[`parser and printer > should format 384.Dockerfile fixture correctly 1`] = `
869869
"LABEL org.opencontainers.image.authors="Jean Luc Picard <[email protected]>"
870870
"
871871
`;
872872
873-
exports[`parser and printer > should format 398.Dockerfile fixtures > 398.Dockerfile 1`] = `
873+
exports[`parser and printer > should format 398.Dockerfile fixture correctly 1`] = `
874874
"ENV a=1 \\
875875
b=2 \\
876876
# comment
@@ -881,7 +881,7 @@ exports[`parser and printer > should format 398.Dockerfile fixtures > 398.Docker
881881
"
882882
`;
883883
884-
exports[`parser and printer > should format 441.Dockerfile fixtures > 441.Dockerfile 1`] = `
884+
exports[`parser and printer > should format 441.Dockerfile fixture correctly with options: {"indent":4} 1`] = `
885885
"RUN \\
886886
# install dependencies
887887
NODE_ENV=production npm install-clean \\
@@ -890,7 +890,9 @@ exports[`parser and printer > should format 441.Dockerfile fixtures > 441.Docker
890890
"
891891
`;
892892
893-
exports[`parser and printer > should format Dockerfile fixtures > Dockerfile 1`] = `
893+
exports[`parser and printer > should format 445.sh fixture correctly 1`] = `"reached EOF without closing quote '"`;
894+
895+
exports[`parser and printer > should format Dockerfile fixture correctly 1`] = `
894896
"FROM node:lts-alpine as builder
895897
896898
# 安装与编译代码
@@ -907,7 +909,7 @@ EXPOSE 2015
907909
"
908910
`;
909911
910-
exports[`parser and printer > should format hosts fixtures > hosts 1`] = `
912+
exports[`parser and printer > should format hosts fixture correctly 1`] = `
911913
"##
912914
# Host Database
913915
#
@@ -924,21 +926,21 @@ exports[`parser and printer > should format hosts fixtures > hosts 1`] = `
924926
"
925927
`;
926928
927-
exports[`parser and printer > should format jvm.options fixtures > jvm.options 1`] = `
929+
exports[`parser and printer > should format jvm.options fixture correctly 1`] = `
928930
"# this is a comment
929931
-mx2048m -XX:MaxPermSize=2048m -Drebel.spring_plugin=true -Drebel.hibernate_plugin=true
930932
931933
idea.cycle.buffer.size = 1024
932934
"
933935
`;
934936
935-
exports[`parser and printer > should format no-ext fixtures > no-ext 1`] = `
937+
exports[`parser and printer > should format no-ext fixture correctly 1`] = `
936938
"#!/usr/bin/env sh
937939
echo 'foo'
938940
"
939941
`;
940942
941-
exports[`parser and printer > should format shell.sh fixtures > shell.sh 1`] = `
943+
exports[`parser and printer > should format shell.sh fixture correctly 1`] = `
942944
"#!/bin/bash
943945
NAME= "ufc-web-client"
944946

packages/sh/test/fixtures.spec.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,41 @@ import { format } from 'prettier'
55

66
import * as sh from 'prettier-plugin-sh'
77

8-
const _dirname = import.meta.dirname
8+
const PARSER_OPTIONS: Partial<
9+
Record<string, Omit<sh.ShPrintOptions, 'originalText'>>
10+
> = {
11+
441: {
12+
indent: 4,
13+
},
14+
}
915

1016
describe('parser and printer', () => {
11-
const fixtures = path.resolve(_dirname, 'fixtures')
17+
const fixtures = path.resolve(import.meta.dirname, 'fixtures')
1218

1319
for (const relativeFilepath of fs.readdirSync(fixtures)) {
1420
const filepath = path.resolve(fixtures, relativeFilepath)
1521
const input = fs.readFileSync(filepath, 'utf8')
1622

17-
it(`should format ${relativeFilepath} fixtures`, async () => {
23+
const caseName = relativeFilepath.slice(
24+
0,
25+
relativeFilepath.lastIndexOf('.'),
26+
)
27+
28+
const overrideOptions = PARSER_OPTIONS[caseName]
29+
30+
it(`should format ${relativeFilepath} fixture correctly${
31+
overrideOptions ? ' with options: ' + JSON.stringify(overrideOptions) : ''
32+
}`, async () => {
1833
try {
1934
const output = await format(input, {
2035
filepath,
2136
plugins: [sh],
37+
...overrideOptions,
2238
})
2339

24-
expect(output).toMatchSnapshot(relativeFilepath)
40+
expect(output).toMatchSnapshot()
2541
} catch (err: unknown) {
26-
expect((err as Error).message.split('\n').at(0)).toMatchSnapshot(
27-
relativeFilepath,
28-
)
42+
expect((err as Error).message.split('\n').at(0)).toMatchSnapshot()
2943
}
3044
})
3145
}

packages/sh/test/fixtures/445.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
curl --location --request PUT 'https://url.com/bla?q=\'hi\'' \
2+
--header 'Content-Type: application/json'
Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,29 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3-
exports[`parser and printer > printer should handle eol correctly > 556.sql 1`] = `""CREATE TABLE \\"test\\" (\\"id\\" uuid NOT NULL)\\r\\nWITH\\r\\n (oids = false);\\r\\n""`;
3+
exports[`parser and printer > printer should handle 556.sql eol correctly with options: {"language":"mysql","endOfLine":"crlf"} 1`] = `
4+
"CREATE TABLE "test" ("id" uuid NOT NULL)
5+
WITH
6+
(oids = false);
7+
"
8+
`;
49

5-
exports[`parser and printer > printer should handle eol correctly > 557.sql 1`] = `""CREATE TABLE \\"test\\" (\\"id\\" uuid NOT NULL)\\rWITH\\r (oids = false);\\r""`;
10+
exports[`parser and printer > printer should handle 557.sql eol correctly with options: {"language":"mysql","endOfLine":"cr"} 1`] = `
11+
"CREATE TABLE "test" ("id" uuid NOT NULL)
12+
WITH
13+
(oids = false);
14+
"
15+
`;
616

7-
exports[`parser and printer > printer should handle eol correctly > 558.sql 1`] = `""CREATE TABLE \\"test\\" (\\"id\\" uuid NOT NULL)\\nWITH\\n (oids = false);\\n""`;
17+
exports[`parser and printer > printer should handle 558.sql eol correctly with options: {"language":"mysql","endOfLine":"lf"} 1`] = `
18+
"CREATE TABLE "test" ("id" uuid NOT NULL)
19+
WITH
20+
(oids = false);
21+
"
22+
`;
823

9-
exports[`parser and printer > printer should handle eol correctly > 559.sql 1`] = `""CREATE TABLE \\"test\\" (\\"id\\" uuid NOT NULL)\\r\\nWITH\\r\\n (oids = false);\\r\\n""`;
24+
exports[`parser and printer > printer should handle 559.sql eol correctly with options: {"language":"mysql","endOfLine":"auto"} 1`] = `
25+
"CREATE TABLE "test" ("id" uuid NOT NULL)
26+
WITH
27+
(oids = false);
28+
"
29+
`;

packages/sql/test/__snapshots__/fixtures.spec.ts.snap

Lines changed: 15 additions & 7 deletions
Large diffs are not rendered by default.
Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import fs from 'node:fs'
22
import path from 'node:path'
3-
import { fileURLToPath } from 'node:url'
43

54
import { format } from 'prettier'
65

76
import SqlPlugin, { type SqlFormatOptions } from 'prettier-plugin-sql'
87

9-
const PARSER_OPTIONS: Record<string, SqlFormatOptions> = {
8+
const PARSER_OPTIONS: Partial<Record<string, SqlFormatOptions>> = {
109
556: {
1110
language: 'mysql',
1211
endOfLine: 'crlf',
@@ -25,31 +24,35 @@ const PARSER_OPTIONS: Record<string, SqlFormatOptions> = {
2524
},
2625
}
2726

28-
const _dirname =
29-
typeof __dirname === 'undefined'
30-
? path.dirname(fileURLToPath(import.meta.url))
31-
: __dirname
32-
3327
describe('parser and printer', () => {
34-
it('printer should handle eol correctly', async () => {
35-
const fixtures = path.resolve(_dirname, 'fixtures-eol')
36-
for (const filepath of fs.readdirSync(fixtures)) {
37-
const input = fs.readFileSync(path.resolve(fixtures, filepath)).toString()
28+
const fixtures = path.resolve(import.meta.dirname, 'fixtures-eol')
29+
30+
for (const relativeFilepath of fs.readdirSync(fixtures)) {
31+
const filepath = path.resolve(fixtures, relativeFilepath)
32+
const input = fs.readFileSync(filepath, 'utf8')
33+
34+
const caseName = relativeFilepath.slice(
35+
0,
36+
relativeFilepath.lastIndexOf('.'),
37+
)
3838

39-
const caseName = filepath.slice(0, filepath.lastIndexOf('.'))
39+
const overrideOptions = PARSER_OPTIONS[caseName]
4040

41+
it(`printer should handle ${relativeFilepath} eol correctly${
42+
overrideOptions ? ' with options: ' + JSON.stringify(overrideOptions) : ''
43+
}`, async () => {
4144
try {
4245
const output = await format(input, {
4346
filepath,
4447
parser: 'sql',
4548
plugins: [SqlPlugin],
46-
...PARSER_OPTIONS[caseName],
49+
...overrideOptions,
4750
})
4851

49-
expect(JSON.stringify(output)).toMatchSnapshot(filepath)
52+
expect(output).toMatchSnapshot()
5053
} catch (error) {
51-
expect(error).toMatchSnapshot(filepath)
54+
expect(error).toMatchSnapshot()
5255
}
53-
}
54-
})
56+
})
57+
}
5558
})

0 commit comments

Comments
 (0)