-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Add more spec tests for the new tokenizer and fix issues in it
- Loading branch information
1 parent
7026fd1
commit 2a6fc8c
Showing
15 changed files
with
465 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { deepStrictEqual } from "assert"; | ||
import { tokenizer } from "../tokenizer.js"; | ||
import { TokenType } from "../types.js"; | ||
|
||
let exampleConf = [ | ||
// | ||
//23456 | ||
"[Host]", // L1 | ||
"@Incremental=yes", // L2 | ||
"KernelCommandLineExtra=systemd.crash_shell=yes", // L3 | ||
" systemd.log_level=debug", // L4 | ||
].join("\n"); | ||
|
||
const repeat = (type: TokenType, times: number) => new Array<TokenType>(times).fill(type); | ||
const tokenTypes = [ | ||
// L1 | ||
TokenType.none, | ||
...repeat(TokenType.section, "[Host]".length - 1), | ||
TokenType.unknown, | ||
// L2 | ||
TokenType.none, | ||
...repeat(TokenType.directiveKey, "@Incremental".length), | ||
...repeat(TokenType.directiveValue, "yes".length), | ||
TokenType.directiveValue, | ||
// L3 | ||
TokenType.none, | ||
...repeat(TokenType.directiveKey, "KernelCommandLineExtra".length), | ||
...repeat(TokenType.directiveValue, "systemd.crash_shell=yes".length), | ||
TokenType.directiveValue, | ||
// L4 | ||
TokenType.none, | ||
...repeat(TokenType.directiveValue, " systemd.log_level=debug".length), | ||
]; | ||
|
||
for (let i = 0; i < tokenTypes.length; i++) { | ||
const tokenType = tokenTypes[i]; | ||
const conf = exampleConf.slice(0, i); | ||
const result = tokenizer(conf, { mkosi: true }); | ||
deepStrictEqual( | ||
result.forecast, | ||
tokenType, | ||
`exampleConf[${i}].tokenType == ${tokenType} (conf: ${JSON.stringify(conf)})` | ||
); | ||
|
||
// const result2 = tokenizer(exampleConf, { cursor: i, mkosi: true }); | ||
// deepStrictEqual(result, result2, `exampleConf[${i}]`); | ||
} | ||
|
||
const prev = tokenizer(exampleConf, { mkosi: true }); | ||
exampleConf += "\n"; | ||
{ | ||
const result = tokenizer(exampleConf, { mkosi: true }); | ||
const result2 = tokenizer(exampleConf, { prevTokens: prev.tokens, mkosi: true }); | ||
deepStrictEqual(result.forecast, TokenType.none); | ||
deepStrictEqual(result, result2); | ||
} | ||
|
||
exampleConf += " "; | ||
{ | ||
console.log(JSON.stringify(exampleConf)); | ||
const result = tokenizer(exampleConf, { mkosi: true }); | ||
const result2 = tokenizer(exampleConf, { prevTokens: prev.tokens, mkosi: true }); | ||
deepStrictEqual(result.forecast, TokenType.directiveValue); | ||
deepStrictEqual(result, result2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { tokenizer } from "../tokenizer.js"; | ||
import { AssertTokens, test } from "./utils.js"; | ||
|
||
let exampleConf = [ | ||
// | ||
//23456 | ||
"[Host]", // L1 | ||
"@Incremental=yes", // L2 | ||
"KernelCommandLineExtra=systemd.crash_shell=yes", // L3 | ||
" systemd.log_level=debug", // L4 | ||
].join("\n"); | ||
|
||
test(exampleConf, ({ diagnosis }) => { | ||
const { tokens } = tokenizer(exampleConf, { mkosi: true }); | ||
diagnosis(tokens); | ||
|
||
const assert = new AssertTokens(tokens); | ||
assert | ||
.section("[Host]") | ||
.key("@Incremental") | ||
.assignment() | ||
.value("yes") | ||
.key("KernelCommandLineExtra") | ||
.assignment() | ||
.value("systemd.crash_shell=yes") | ||
.value("systemd.log_level=debug"); | ||
}); | ||
|
||
test(exampleConf, ({ diagnosis }) => { | ||
const { tokens } = tokenizer(exampleConf, {}); | ||
diagnosis(tokens); | ||
|
||
const assert = new AssertTokens(tokens); | ||
assert | ||
.section("[Host]") | ||
.key("@Incremental") | ||
.assignment() | ||
.value("yes") | ||
.key("KernelCommandLineExtra") | ||
.assignment() | ||
.value("systemd.crash_shell=yes") | ||
.key("systemd.log_level") | ||
.assignment() | ||
.value('debug'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { deepStrictEqual } from "assert"; | ||
import { tokenizer } from "../tokenizer.js"; | ||
import { AssertTokens, test } from "./utils.js"; | ||
import { TokenType } from "../types.js"; | ||
|
||
test("[Host] ", ({ conf, diagnosis, range }) => { | ||
const { tokens, forecast } = tokenizer(conf, { mkosi: true }); | ||
diagnosis(tokens); | ||
deepStrictEqual(tokens, [ | ||
{ type: TokenType.section, range: range(0, 6), text: "[Host]" }, | ||
{ type: TokenType.unknown, range: range(6, 7), text: " " }, | ||
]); | ||
deepStrictEqual(forecast, TokenType.unknown); | ||
}); | ||
|
||
test("[Host] #", ({ conf, diagnosis, range }) => { | ||
const { tokens, forecast } = tokenizer(conf, { mkosi: true }); | ||
diagnosis(tokens); | ||
deepStrictEqual(tokens, [ | ||
{ type: TokenType.section, range: range(0, 6), text: "[Host]" }, | ||
{ type: TokenType.unknown, range: range(6, 7), text: " " }, | ||
{ type: TokenType.comment, range: range(7, 8), text: "#" }, | ||
]); | ||
deepStrictEqual(forecast, TokenType.comment); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { readFileSync } from "fs"; | ||
import { resolve } from "path"; | ||
import { tokenizer } from "../tokenizer"; | ||
import { getDirectivesFromTokens } from "../get-directive-keys"; | ||
import { AssertTokens } from "./utils"; | ||
|
||
const filePath = resolve(__dirname, "../../../test/samples/mkosi/mkosi/mkosi.conf"); | ||
const fileContent = readFileSync(filePath, "utf-8"); | ||
const result = tokenizer(fileContent, { mkosi: true }); | ||
console.log(`tokens.length = ${result.tokens.length}`); | ||
|
||
new AssertTokens(result.tokens) | ||
.comment() | ||
.section("[Output]") | ||
.comment() | ||
.comment() | ||
.key("@Format") | ||
.assignment() | ||
.value("directory") | ||
.key("@CacheDirectory") | ||
.assignment() | ||
.value("mkosi.cache") | ||
.key("@OutputDirectory") | ||
.assignment() | ||
.value("mkosi.output") | ||
// | ||
.section("[Content]") | ||
.key("Autologin") | ||
.assignment() | ||
.value("yes") | ||
.key("@SELinuxRelabel") | ||
.assignment() | ||
.value("no") | ||
.key("@ShimBootloader") | ||
.assignment() | ||
.value("unsigned") | ||
.key("BuildSources") | ||
.assignment() | ||
.value(".") | ||
.key("BuildSourcesEphemeral") | ||
.assignment() | ||
.value("yes") | ||
// | ||
.key("Packages") | ||
.assignment() | ||
.value("attr") | ||
.value("ca-certificates") | ||
.value("gdb") | ||
.value("jq") | ||
.value("less") | ||
.value("nano") | ||
.value("strace") | ||
.value("tmux") | ||
// | ||
.key("InitrdPackages") | ||
.assignment() | ||
.value("less") | ||
// | ||
.key("RemoveFiles") | ||
.assignment() | ||
.comment() | ||
.value("/usr/lib/kernel/install.d/20-grub.install") | ||
.comment() | ||
.value("/usr/lib/kernel/install.d/50-dracut.install") | ||
.comment() | ||
// | ||
.key("KernelCommandLine") | ||
.assignment() | ||
.value("console=ttyS0 enforcing=0") | ||
// | ||
.section("[Host]") | ||
.key("@QemuMem") | ||
.assignment() | ||
.value("4G"); | ||
|
||
// console.log(result.tokens); | ||
// const directives = getDirectivesFromTokens(result.tokens); | ||
// console.log(directives); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { deepStrictEqual } from "assert"; | ||
import { tokenizer } from "../tokenizer.js"; | ||
import { AssertTokens, test } from "./utils.js"; | ||
import { TokenType } from "../types.js"; | ||
|
||
test('Key = Value ', ({ conf, diagnosis }) => { | ||
const prev = tokenizer(conf.slice(0, 6)); | ||
|
||
const { tokens, forecast } = tokenizer(conf, { prevTokens: prev.tokens }); | ||
diagnosis(tokens); | ||
|
||
deepStrictEqual(forecast, TokenType.directiveValue); | ||
new AssertTokens(tokens).key('Key ').assignment().value('Value '); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { deepStrictEqual } from "assert"; | ||
import { tokenizer } from "../tokenizer.js"; | ||
import { TokenType } from "../types.js"; | ||
|
||
let exampleConf = [ | ||
// | ||
"[A", // L1 | ||
"", // L2 | ||
].join("\n"); | ||
|
||
const result1 = tokenizer(exampleConf); | ||
deepStrictEqual(result1.forecast, TokenType.none); | ||
|
||
exampleConf = [ | ||
// | ||
"[A\\", // L1 | ||
"", // L2 | ||
].join("\n"); | ||
const result2 = tokenizer(exampleConf); | ||
deepStrictEqual(result2.forecast, TokenType.none); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { deepStrictEqual } from "assert"; | ||
import { tokenizer } from "../tokenizer.js"; | ||
import { TokenType } from "../types.js"; | ||
import { AssertTokens, test } from "./utils.js"; | ||
|
||
let exampleConf = [ | ||
// | ||
"[A]", // L1 | ||
"===", // L2 | ||
"=", // L3 | ||
"LOL", // L4 | ||
].join("\n"); | ||
|
||
test(exampleConf, ({ conf, diagnosis }) => { | ||
const result1 = tokenizer(conf); | ||
diagnosis(result1); | ||
|
||
new AssertTokens(result1.tokens).section("[A]").assignment().value("==").assignment().key("LOL"); | ||
deepStrictEqual(result1.forecast, TokenType.directiveKey); | ||
}); | ||
|
||
test("A=\\\n =C", ({ conf, diagnosis }) => { | ||
const result1 = tokenizer(conf); | ||
diagnosis(result1); | ||
|
||
new AssertTokens(result1.tokens).key("A").assignment().value("\\").value(" =C"); | ||
deepStrictEqual(result1.forecast, TokenType.directiveValue); | ||
}); | ||
|
||
test("A=A\n =C", ({ conf, diagnosis }) => { | ||
const result1 = tokenizer(conf, { mkosi: true }); | ||
diagnosis(result1); | ||
|
||
new AssertTokens(result1.tokens).key("A").assignment().value("A").value("=C"); | ||
deepStrictEqual(result1.forecast, TokenType.directiveValue); | ||
|
||
const result2 = tokenizer(conf); | ||
diagnosis(result2); | ||
new AssertTokens(result2.tokens).key("A").assignment().value("A").assignment().value("C"); | ||
deepStrictEqual(result2.forecast, TokenType.directiveValue); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { deepStrictEqual } from "assert"; | ||
import { tokenizer } from "../tokenizer.js"; | ||
import { TokenType } from "../types.js"; | ||
import { AssertTokens, test } from "./utils.js"; | ||
|
||
test('K=\\\n[V]', ({ conf, diagnosis }) => { | ||
const result1 = tokenizer(conf); | ||
diagnosis(result1); | ||
|
||
new AssertTokens(result1.tokens).key("K").assignment().value("\\").value("[V]"); | ||
deepStrictEqual(result1.forecast, TokenType.directiveValue); | ||
|
||
|
||
const result2 = tokenizer(conf, { mkosi: true }); | ||
diagnosis(result2); | ||
|
||
new AssertTokens(result2.tokens).key("K").assignment().value("\\").section("[V]"); | ||
deepStrictEqual(result2.forecast, TokenType.unknown); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.