Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

site-data-references.js 4.3 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
  1. const { isEqual, get, uniqWith } = require('lodash')
  2. const loadSiteData = require('../../lib/site-data')
  3. const { loadPages } = require('../../lib/pages')
  4. const getDataReferences = require('../../lib/get-liquid-data-references')
  5. const frontmatter = require('../../lib/read-frontmatter')
  6. const fs = require('fs').promises
  7. const path = require('path')
  8. const readFileAsync = require('../../lib/readfile-async')
  9. describe('data references', () => {
  10. jest.setTimeout(60 * 1000)
  11. let data, pages
  12. beforeAll(async (done) => {
  13. data = await loadSiteData()
  14. pages = await loadPages()
  15. pages = pages.filter(page => page.languageCode === 'en')
  16. done()
  17. })
  18. test('every data reference found in English content files is defined and has a value', () => {
  19. let errors = []
  20. expect(pages.length).toBeGreaterThan(0)
  21. pages.forEach(page => {
  22. const file = path.join('content', page.relativePath)
  23. const pageRefs = getDataReferences(page.markdown)
  24. pageRefs.forEach(key => {
  25. const value = get(data.en, key)
  26. if (typeof value !== 'string') errors.push({ key, value, file })
  27. })
  28. })
  29. errors = uniqWith(errors, isEqual) // remove duplicates
  30. expect(errors.length, JSON.stringify(errors, null, 2)).toBe(0)
  31. })
  32. test('every data reference found in metadata of English content files is defined and has a value', async () => {
  33. let errors = []
  34. expect(pages.length).toBeGreaterThan(0)
  35. await Promise.all(pages.map(async page => {
  36. const metadataFile = path.join('content', page.relativePath)
  37. const fileContents = await readFileAsync(path.join(__dirname, '../..', metadataFile))
  38. const { data: metadata } = frontmatter(fileContents, { filepath: page.fullPath })
  39. const metadataRefs = getDataReferences(JSON.stringify(metadata))
  40. metadataRefs.forEach(key => {
  41. const value = get(data.en, key)
  42. if (typeof value !== 'string') errors.push({ key, value, metadataFile })
  43. })
  44. }))
  45. errors = uniqWith(errors, isEqual) // remove duplicates
  46. expect(errors.length, JSON.stringify(errors, null, 2)).toBe(0)
  47. })
  48. test('every data reference found in English reusable files is defined and has a value', async () => {
  49. let errors = []
  50. const allReusables = data.en.site.data.reusables
  51. const reusables = Object.values(allReusables)
  52. expect(reusables.length).toBeGreaterThan(0)
  53. await Promise.all(reusables.map(async reusablesPerFile => {
  54. let reusableFile = path.join(__dirname, '../../data/reusables/', getFilenameByValue(allReusables, reusablesPerFile))
  55. reusableFile = await getFilepath(reusableFile)
  56. const reusableRefs = getDataReferences(JSON.stringify(reusablesPerFile))
  57. reusableRefs.forEach(key => {
  58. const value = get(data.en, key)
  59. if (typeof value !== 'string') errors.push({ key, value, reusableFile })
  60. })
  61. }))
  62. errors = uniqWith(errors, isEqual) // remove duplicates
  63. expect(errors.length, JSON.stringify(errors, null, 2)).toBe(0)
  64. })
  65. test('every data reference found in English variable files is defined and has a value', async () => {
  66. let errors = []
  67. const allVariables = data.en.site.data.variables
  68. const variables = Object.values(allVariables)
  69. expect(variables.length).toBeGreaterThan(0)
  70. await Promise.all(variables.map(async variablesPerFile => {
  71. let variableFile = path.join(__dirname, '../../data/variables/', getFilenameByValue(allVariables, variablesPerFile))
  72. variableFile = await getFilepath(variableFile)
  73. const variableRefs = getDataReferences(JSON.stringify(variablesPerFile))
  74. variableRefs.forEach(key => {
  75. const value = get(data.en, key)
  76. if (typeof value !== 'string') errors.push({ key, value, variableFile })
  77. })
  78. }))
  79. errors = uniqWith(errors, isEqual) // remove duplicates
  80. expect(errors.length, JSON.stringify(errors, null, 2)).toBe(0)
  81. })
  82. })
  83. function getFilenameByValue (object, value) {
  84. return Object.keys(object).find(key => object[key] === value)
  85. }
  86. // if path exists, assume it's a directory; otherwise, assume a YML extension
  87. async function getFilepath (filepath) {
  88. try {
  89. await fs.stat(filepath)
  90. filepath = filepath + '/'
  91. } catch (_) {
  92. filepath = filepath + '.yml'
  93. }
  94. // we only need the relative path
  95. return filepath.replace(path.join(__dirname, '../../'), '')
  96. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...