forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-changelog.js
More file actions
146 lines (126 loc) · 4.19 KB
/
build-changelog.js
File metadata and controls
146 lines (126 loc) · 4.19 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import yaml from 'js-yaml'
import fs from 'fs/promises'
import MockDate from 'mockdate'
import {
createChangelogEntry,
cleanPreviewTitle,
previewAnchor,
prependDatedEntry,
} from '../scripts/build-changelog.js'
import readJsonFile from '../../../lib/read-json-file.js'
const expectedChangelogEntry = readJsonFile('src/graphql/tests/fixtures/changelog-entry.json')
const expectedUpdatedChangelogFile = readJsonFile(
'src/graphql/tests/fixtures/updated-changelog-file.json',
)
describe('creating a changelog from old schema and new schema', () => {
afterEach(() => {
MockDate.reset()
})
it('finds a diff of schema changes, upcoming changes, and preview changes', async () => {
const oldSchemaString = `
type PreviewType {
field1(changeTypeArgument: Int): Int
}
type Query {
stableField: String
removedField: Boolean
argumentsField(
removedRequiredArgument: Int!
removedOptionalArgument: Int!
argumentMadeRequired: Int
argumentMadeOptional: Int!
): String
previewField: PreviewType
}
`
const newSchemaString = `
type PreviewType {
field1(changeTypeArgument: Float): Int
}
type Query {
stableField: String
argumentsField(
argumentMadeRequired: Int!
argumentMadeOptional: Int
): String
previewField: PreviewType!
}
`
const previews = yaml.load(`
- title: Test preview
description: This preview is just for test
toggled_by: ':test_preview'
announcement: null
updates: null
toggled_on:
- PreviewType
- Query.previewField
owning_teams:
- '@github/engineering'
`)
const oldUpcomingChanges = yaml.load(`
upcoming_changes:
- location: EnterprisePendingCollaboratorEdge.isUnlicensed
description: '\`isUnlicensed\` will be removed.'
date: '2021-01-01T00:00:00+00:00'
`).upcoming_changes
const newUpcomingChanges = yaml.load(`
upcoming_changes:
- location: Query.stableField
description: '\`stableField\` will be removed.'
date: '2021-06-01T00:00:00+00:00'
- location: EnterprisePendingCollaboratorEdge.isUnlicensed
description: '\`isUnlicensed\` will be removed.'
date: '2021-01-01T00:00:00+00:00'
`).upcoming_changes
const entry = await createChangelogEntry(
oldSchemaString,
newSchemaString,
previews,
oldUpcomingChanges,
newUpcomingChanges,
)
expect(entry).toEqual(expectedChangelogEntry)
})
it('returns null when there isnt any difference', async () => {
const schemaString = `
type Query {
i: Int!
}`
const nullEntry = await createChangelogEntry(schemaString, schemaString, [], [], [])
expect(nullEntry).toBeNull()
})
})
describe('Preparing preview links', () => {
it('fixes preview names', () => {
// These two are special cases
expect(cleanPreviewTitle('UpdateRefsPreview')).toEqual('Update refs preview')
expect(cleanPreviewTitle('MergeInfoPreview')).toEqual('Merge info preview')
// Previews that don't end in " preview" have it added
expect(cleanPreviewTitle('something interesting')).toEqual('something interesting preview')
// Other things are left as-is
expect(cleanPreviewTitle('nice preview')).toEqual('nice preview')
})
it('creates anchors from preview titles', () => {
expect(previewAnchor('Merge info preview')).toEqual('merge-info-preview')
expect(previewAnchor('some.punct123 preview')).toEqual('somepunct123-preview')
})
})
describe('updating the changelog file', () => {
afterEach(() => {
MockDate.reset()
})
it('modifies the entry object and the file on disk', async () => {
const testTargetPath = 'src/graphql/tests/fixtures/example-changelog.json'
const previousContents = await fs.readFile(testTargetPath)
const exampleEntry = { someStuff: true }
const expectedDate = '2020-11-20'
MockDate.set(expectedDate)
prependDatedEntry(exampleEntry, testTargetPath)
const newContents = await fs.readFile(testTargetPath, 'utf8')
// reset the file:
await fs.writeFile(testTargetPath, previousContents)
expect(exampleEntry).toEqual({ someStuff: true, date: expectedDate })
expect(JSON.parse(newContents)).toEqual(expectedUpdatedChangelogFile)
})
})