MCPcopy Index your code
hub / github.com/SchemaStore/schemastore / assertTestFileHasSchemaPragma

Function assertTestFileHasSchemaPragma

cli.js:1538–1610  ·  view source on GitHub ↗
(
  /** @type {SchemaFile} */ schemaFile,
  /** @type {DataFile} */ testFile,
  /** @type {Ora} */ spinner,
)

Source from the content-addressed store, hash-verified

1536}
1537
1538async function assertTestFileHasSchemaPragma(
1539 /** @type {SchemaFile} */ schemaFile,
1540 /** @type {DataFile} */ testFile,
1541 /** @type {Ora} */ spinner,
1542) {
1543 if (testFile.path.endsWith('yaml') || testFile.path.endsWith('yml')) {
1544 const firstLine = await readFirstLine(testFile.path)
1545 const expected = `# yaml-language-server: $schema=${path.relative(path.dirname(testFile.path), schemaFile.path).replaceAll('\\', '/')}`
1546
1547 if (firstLine !== expected) {
1548 if (argv.fix) {
1549 spinner.info(`Fixing pragma for file "${testFile.path}"`)
1550 if (firstLine.includes('yaml-language-server')) {
1551 const oldContent = await fs.readFile(testFile.path, 'utf-8')
1552 const newContent =
1553 expected + '\n' + oldContent.slice(oldContent.indexOf('\n') + 1)
1554 await fs.writeFile(testFile.path, newContent)
1555 } else {
1556 const newContent =
1557 expected + '\n' + (await fs.readFile(testFile.path, 'utf-8'))
1558 await fs.writeFile(testFile.path, newContent)
1559 }
1560 } else {
1561 spinner.stop()
1562 printErrorAndExit(new Error(), [
1563 `Failed to find schema pragma for YAML File "./${testFile.path}"`,
1564 `Expected first line of file to be "${expected}"`,
1565 `But, found first line of file to be "${firstLine}"`,
1566 `Append "--fix" to the command line to automatically fix all fixable issues`,
1567 ])
1568 }
1569 }
1570 } else if (testFile.path.endsWith('.toml')) {
1571 const firstLine = await readFirstLine(testFile.path)
1572 const expected = `#:schema ${path.relative(path.dirname(testFile.path), schemaFile.path).replaceAll('\\', '/')}`
1573
1574 if (firstLine !== expected) {
1575 if (argv.fix) {
1576 spinner.info(`Fixing pragma for file "${testFile.path}"`)
1577 if (firstLine.includes('#:schema')) {
1578 const oldContent = await fs.readFile(testFile.path, 'utf-8')
1579 const newContent =
1580 expected + '\n' + oldContent.slice(oldContent.indexOf('\n') + 1)
1581 await fs.writeFile(testFile.path, newContent)
1582 } else {
1583 const newContent =
1584 expected + '\n' + (await fs.readFile(testFile.path, 'utf-8'))
1585 await fs.writeFile(testFile.path, newContent)
1586 }
1587 } else {
1588 spinner.stop()
1589 printErrorAndExit(new Error(), [
1590 `Failed to find schema pragma for TOML File "./${testFile.path}"`,
1591 `Expected first line of file to be "${expected}"`,
1592 `But, found first line of file to be "${firstLine}"`,
1593 `Append "--fix" to the command line to automatically fix all fixable issues`,
1594 ])
1595 }

Callers 2

onPositiveTestFileFunction · 0.85
onNegativeTestFileFunction · 0.85

Calls 2

readFirstLineFunction · 0.85
printErrorAndExitFunction · 0.85

Tested by

no test coverage detected