2022-03-02 05:58:18 +00:00
|
|
|
import * as fs from 'fs'
|
|
|
|
import * as os from 'os'
|
|
|
|
import path from 'path'
|
2022-05-05 17:29:20 +00:00
|
|
|
import {summary, SUMMARY_ENV_VAR} from '../src/summary'
|
2022-03-02 05:58:18 +00:00
|
|
|
|
2022-05-05 17:29:20 +00:00
|
|
|
const testDirectoryPath = path.join(__dirname, 'test')
|
|
|
|
const testFilePath = path.join(testDirectoryPath, 'test-summary.md')
|
2022-03-02 05:58:18 +00:00
|
|
|
|
|
|
|
async function assertSummary(expected: string): Promise<void> {
|
|
|
|
const file = await fs.promises.readFile(testFilePath, {encoding: 'utf8'})
|
|
|
|
expect(file).toEqual(expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
const fixtures = {
|
|
|
|
text: 'hello world 🌎',
|
|
|
|
code: `func fork() {
|
|
|
|
for {
|
|
|
|
go fork()
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
list: ['foo', 'bar', 'baz', '💣'],
|
|
|
|
table: [
|
|
|
|
[
|
|
|
|
{
|
|
|
|
data: 'foo',
|
|
|
|
header: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
data: 'bar',
|
|
|
|
header: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
data: 'baz',
|
|
|
|
header: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
data: 'tall',
|
|
|
|
rowspan: '3'
|
|
|
|
}
|
|
|
|
],
|
|
|
|
['one', 'two', 'three'],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
data: 'wide',
|
|
|
|
colspan: '3'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
],
|
|
|
|
details: {
|
|
|
|
label: 'open me',
|
|
|
|
content: '🎉 surprise'
|
|
|
|
},
|
|
|
|
img: {
|
|
|
|
src: 'https://github.com/actions.png',
|
|
|
|
alt: 'actions logo',
|
|
|
|
options: {
|
|
|
|
width: '32',
|
|
|
|
height: '32'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
quote: {
|
|
|
|
text: 'Where the world builds software',
|
|
|
|
cite: 'https://github.com/about'
|
2022-03-02 17:10:01 +00:00
|
|
|
},
|
|
|
|
link: {
|
|
|
|
text: 'GitHub',
|
|
|
|
href: 'https://github.com/'
|
2022-03-02 05:58:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-05 17:29:20 +00:00
|
|
|
describe('@actions/core/src/summary', () => {
|
2022-03-02 05:58:18 +00:00
|
|
|
beforeEach(async () => {
|
2022-03-03 04:43:51 +00:00
|
|
|
process.env[SUMMARY_ENV_VAR] = testFilePath
|
2022-05-05 17:29:20 +00:00
|
|
|
await fs.promises.mkdir(testDirectoryPath, {recursive: true})
|
2022-03-02 05:58:18 +00:00
|
|
|
await fs.promises.writeFile(testFilePath, '', {encoding: 'utf8'})
|
2022-05-05 17:29:20 +00:00
|
|
|
summary.emptyBuffer()
|
2022-03-02 05:58:18 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
await fs.promises.unlink(testFilePath)
|
|
|
|
})
|
|
|
|
|
2022-03-03 04:43:51 +00:00
|
|
|
it('throws if summary env var is undefined', async () => {
|
|
|
|
process.env[SUMMARY_ENV_VAR] = undefined
|
2022-05-05 17:29:20 +00:00
|
|
|
const write = summary.addRaw(fixtures.text).write()
|
2022-03-03 04:43:51 +00:00
|
|
|
|
|
|
|
await expect(write).rejects.toThrow()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('throws if summary file does not exist', async () => {
|
|
|
|
await fs.promises.unlink(testFilePath)
|
2022-05-05 17:29:20 +00:00
|
|
|
const write = summary.addRaw(fixtures.text).write()
|
2022-03-03 04:43:51 +00:00
|
|
|
|
|
|
|
await expect(write).rejects.toThrow()
|
|
|
|
})
|
|
|
|
|
2022-03-02 05:58:18 +00:00
|
|
|
it('appends text to summary file', async () => {
|
|
|
|
await fs.promises.writeFile(testFilePath, '# ', {encoding: 'utf8'})
|
2022-05-05 17:29:20 +00:00
|
|
|
await summary.addRaw(fixtures.text).write()
|
2022-03-02 05:58:18 +00:00
|
|
|
await assertSummary(`# ${fixtures.text}`)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('overwrites text to summary file', async () => {
|
|
|
|
await fs.promises.writeFile(testFilePath, 'overwrite', {encoding: 'utf8'})
|
2022-05-05 17:29:20 +00:00
|
|
|
await summary.addRaw(fixtures.text).write({overwrite: true})
|
2022-03-02 05:58:18 +00:00
|
|
|
await assertSummary(fixtures.text)
|
|
|
|
})
|
|
|
|
|
2022-03-03 04:49:17 +00:00
|
|
|
it('appends text with EOL to summary file', async () => {
|
|
|
|
await fs.promises.writeFile(testFilePath, '# ', {encoding: 'utf8'})
|
2022-05-05 17:29:20 +00:00
|
|
|
await summary.addRaw(fixtures.text, true).write()
|
2022-03-03 04:49:17 +00:00
|
|
|
await assertSummary(`# ${fixtures.text}${os.EOL}`)
|
|
|
|
})
|
|
|
|
|
2022-03-02 05:58:18 +00:00
|
|
|
it('chains appends text to summary file', async () => {
|
|
|
|
await fs.promises.writeFile(testFilePath, '', {encoding: 'utf8'})
|
2022-05-05 17:29:20 +00:00
|
|
|
await summary
|
2022-03-03 04:49:17 +00:00
|
|
|
.addRaw(fixtures.text)
|
|
|
|
.addRaw(fixtures.text)
|
|
|
|
.addRaw(fixtures.text)
|
2022-03-02 05:58:18 +00:00
|
|
|
.write()
|
|
|
|
await assertSummary([fixtures.text, fixtures.text, fixtures.text].join(''))
|
|
|
|
})
|
|
|
|
|
|
|
|
it('empties buffer after write', async () => {
|
|
|
|
await fs.promises.writeFile(testFilePath, '', {encoding: 'utf8'})
|
2022-05-05 17:29:20 +00:00
|
|
|
await summary.addRaw(fixtures.text).write()
|
2022-03-02 05:58:18 +00:00
|
|
|
await assertSummary(fixtures.text)
|
2022-05-05 17:29:20 +00:00
|
|
|
expect(summary.isEmptyBuffer()).toBe(true)
|
2022-03-02 05:58:18 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('returns summary buffer as string', () => {
|
2022-05-05 17:29:20 +00:00
|
|
|
summary.addRaw(fixtures.text)
|
|
|
|
expect(summary.stringify()).toEqual(fixtures.text)
|
2022-03-02 05:58:18 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('return correct values for isEmptyBuffer', () => {
|
2022-05-05 17:29:20 +00:00
|
|
|
summary.addRaw(fixtures.text)
|
|
|
|
expect(summary.isEmptyBuffer()).toBe(false)
|
2022-03-02 05:58:18 +00:00
|
|
|
|
2022-05-05 17:29:20 +00:00
|
|
|
summary.emptyBuffer()
|
|
|
|
expect(summary.isEmptyBuffer()).toBe(true)
|
2022-03-02 05:58:18 +00:00
|
|
|
})
|
|
|
|
|
2022-03-03 04:56:30 +00:00
|
|
|
it('clears a buffer and summary file', async () => {
|
|
|
|
await fs.promises.writeFile(testFilePath, 'content', {encoding: 'utf8'})
|
2022-05-05 17:29:20 +00:00
|
|
|
await summary.clear()
|
2022-03-03 04:56:30 +00:00
|
|
|
await assertSummary('')
|
2022-05-05 17:29:20 +00:00
|
|
|
expect(summary.isEmptyBuffer()).toBe(true)
|
2022-03-03 04:56:30 +00:00
|
|
|
})
|
|
|
|
|
2022-03-02 05:58:18 +00:00
|
|
|
it('adds EOL', async () => {
|
2023-08-03 20:36:11 +00:00
|
|
|
await summary.addRaw(fixtures.text).addEOL().write()
|
2022-03-02 05:58:18 +00:00
|
|
|
await assertSummary(fixtures.text + os.EOL)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('adds a code block without language', async () => {
|
2022-05-05 17:29:20 +00:00
|
|
|
await summary.addCodeBlock(fixtures.code).write()
|
2022-03-02 05:58:18 +00:00
|
|
|
const expected = `<pre><code>func fork() {\n for {\n go fork()\n }\n}</code></pre>${os.EOL}`
|
|
|
|
await assertSummary(expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('adds a code block with a language', async () => {
|
2022-05-05 17:29:20 +00:00
|
|
|
await summary.addCodeBlock(fixtures.code, 'go').write()
|
2022-03-02 05:58:18 +00:00
|
|
|
const expected = `<pre lang="go"><code>func fork() {\n for {\n go fork()\n }\n}</code></pre>${os.EOL}`
|
|
|
|
await assertSummary(expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('adds an unordered list', async () => {
|
2022-05-05 17:29:20 +00:00
|
|
|
await summary.addList(fixtures.list).write()
|
2022-03-02 05:58:18 +00:00
|
|
|
const expected = `<ul><li>foo</li><li>bar</li><li>baz</li><li>💣</li></ul>${os.EOL}`
|
|
|
|
await assertSummary(expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('adds an ordered list', async () => {
|
2022-05-05 17:29:20 +00:00
|
|
|
await summary.addList(fixtures.list, true).write()
|
2022-03-02 05:58:18 +00:00
|
|
|
const expected = `<ol><li>foo</li><li>bar</li><li>baz</li><li>💣</li></ol>${os.EOL}`
|
|
|
|
await assertSummary(expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('adds a table', async () => {
|
2022-05-05 17:29:20 +00:00
|
|
|
await summary.addTable(fixtures.table).write()
|
2022-03-02 05:58:18 +00:00
|
|
|
const expected = `<table><tr><th>foo</th><th>bar</th><th>baz</th><td rowspan="3">tall</td></tr><tr><td>one</td><td>two</td><td>three</td></tr><tr><td colspan="3">wide</td></tr></table>${os.EOL}`
|
|
|
|
await assertSummary(expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('adds a details element', async () => {
|
2022-05-05 17:29:20 +00:00
|
|
|
await summary
|
2022-03-02 05:58:18 +00:00
|
|
|
.addDetails(fixtures.details.label, fixtures.details.content)
|
|
|
|
.write()
|
|
|
|
const expected = `<details><summary>open me</summary>🎉 surprise</details>${os.EOL}`
|
|
|
|
await assertSummary(expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('adds an image with alt text', async () => {
|
2022-05-05 17:29:20 +00:00
|
|
|
await summary.addImage(fixtures.img.src, fixtures.img.alt).write()
|
2022-03-02 05:58:18 +00:00
|
|
|
const expected = `<img src="https://github.com/actions.png" alt="actions logo">${os.EOL}`
|
|
|
|
await assertSummary(expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('adds an image with custom dimensions', async () => {
|
2022-05-05 17:29:20 +00:00
|
|
|
await summary
|
2022-03-02 05:58:18 +00:00
|
|
|
.addImage(fixtures.img.src, fixtures.img.alt, fixtures.img.options)
|
|
|
|
.write()
|
|
|
|
const expected = `<img src="https://github.com/actions.png" alt="actions logo" width="32" height="32">${os.EOL}`
|
|
|
|
await assertSummary(expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('adds an image with custom dimensions', async () => {
|
2022-05-05 17:29:20 +00:00
|
|
|
await summary
|
2022-03-02 05:58:18 +00:00
|
|
|
.addImage(fixtures.img.src, fixtures.img.alt, fixtures.img.options)
|
|
|
|
.write()
|
|
|
|
const expected = `<img src="https://github.com/actions.png" alt="actions logo" width="32" height="32">${os.EOL}`
|
|
|
|
await assertSummary(expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('adds headings h1...h6', async () => {
|
|
|
|
for (const i of [1, 2, 3, 4, 5, 6]) {
|
2022-05-05 17:29:20 +00:00
|
|
|
summary.addHeading('heading', i)
|
2022-03-02 05:58:18 +00:00
|
|
|
}
|
2022-05-05 17:29:20 +00:00
|
|
|
await summary.write()
|
2022-03-02 05:58:18 +00:00
|
|
|
const expected = `<h1>heading</h1>${os.EOL}<h2>heading</h2>${os.EOL}<h3>heading</h3>${os.EOL}<h4>heading</h4>${os.EOL}<h5>heading</h5>${os.EOL}<h6>heading</h6>${os.EOL}`
|
|
|
|
await assertSummary(expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('adds h1 if heading level not specified', async () => {
|
2022-05-05 17:29:20 +00:00
|
|
|
await summary.addHeading('heading').write()
|
2022-03-02 05:58:18 +00:00
|
|
|
const expected = `<h1>heading</h1>${os.EOL}`
|
|
|
|
await assertSummary(expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('uses h1 if heading level is garbage or out of range', async () => {
|
2022-05-05 17:29:20 +00:00
|
|
|
await summary
|
2022-03-02 05:58:18 +00:00
|
|
|
.addHeading('heading', 'foobar')
|
|
|
|
.addHeading('heading', 1337)
|
|
|
|
.addHeading('heading', -1)
|
|
|
|
.addHeading('heading', Infinity)
|
|
|
|
.write()
|
|
|
|
const expected = `<h1>heading</h1>${os.EOL}<h1>heading</h1>${os.EOL}<h1>heading</h1>${os.EOL}<h1>heading</h1>${os.EOL}`
|
|
|
|
await assertSummary(expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('adds a separator', async () => {
|
2022-05-05 17:29:20 +00:00
|
|
|
await summary.addSeparator().write()
|
2022-03-02 05:58:18 +00:00
|
|
|
const expected = `<hr>${os.EOL}`
|
|
|
|
await assertSummary(expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('adds a break', async () => {
|
2022-05-05 17:29:20 +00:00
|
|
|
await summary.addBreak().write()
|
2022-03-02 05:58:18 +00:00
|
|
|
const expected = `<br>${os.EOL}`
|
|
|
|
await assertSummary(expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('adds a quote', async () => {
|
2022-05-05 17:29:20 +00:00
|
|
|
await summary.addQuote(fixtures.quote.text).write()
|
2022-03-02 05:58:18 +00:00
|
|
|
const expected = `<blockquote>Where the world builds software</blockquote>${os.EOL}`
|
|
|
|
await assertSummary(expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('adds a quote with citation', async () => {
|
2022-05-05 17:29:20 +00:00
|
|
|
await summary.addQuote(fixtures.quote.text, fixtures.quote.cite).write()
|
2022-03-02 05:58:18 +00:00
|
|
|
const expected = `<blockquote cite="https://github.com/about">Where the world builds software</blockquote>${os.EOL}`
|
|
|
|
await assertSummary(expected)
|
|
|
|
})
|
2022-03-02 17:10:01 +00:00
|
|
|
|
|
|
|
it('adds a link with href', async () => {
|
2022-05-05 17:29:20 +00:00
|
|
|
await summary.addLink(fixtures.link.text, fixtures.link.href).write()
|
2022-03-02 17:10:01 +00:00
|
|
|
const expected = `<a href="https://github.com/">GitHub</a>${os.EOL}`
|
|
|
|
await assertSummary(expected)
|
|
|
|
})
|
2022-03-02 05:58:18 +00:00
|
|
|
})
|