1
0
Fork 0

add -> addRaw

pull/1014/head
Rob Herley 2022-03-02 23:49:17 -05:00
parent ec5c955c0a
commit d27bf857e6
No known key found for this signature in database
GPG Key ID: D1602042C3543B06
2 changed files with 34 additions and 27 deletions

View File

@ -84,61 +84,67 @@ describe('@actions/core/src/markdown-summary', () => {
it('throws if summary env var is undefined', async () => { it('throws if summary env var is undefined', async () => {
process.env[SUMMARY_ENV_VAR] = undefined process.env[SUMMARY_ENV_VAR] = undefined
const write = markdownSummary.add(fixtures.text).write() const write = markdownSummary.addRaw(fixtures.text).write()
await expect(write).rejects.toThrow() await expect(write).rejects.toThrow()
}) })
it('throws if summary file does not exist', async () => { it('throws if summary file does not exist', async () => {
await fs.promises.unlink(testFilePath) await fs.promises.unlink(testFilePath)
const write = markdownSummary.add(fixtures.text).write() const write = markdownSummary.addRaw(fixtures.text).write()
await expect(write).rejects.toThrow() await expect(write).rejects.toThrow()
}) })
it('throws if write will exceed file limit', async () => { it('throws if write will exceed file limit', async () => {
const aaa = 'a'.repeat(SUMMARY_LIMIT_BYTES + 1) const aaa = 'a'.repeat(SUMMARY_LIMIT_BYTES + 1)
const write = markdownSummary.add(aaa).write() const write = markdownSummary.addRaw(aaa).write()
await expect(write).rejects.toThrow() await expect(write).rejects.toThrow()
}) })
it('appends text to summary file', async () => { it('appends text to summary file', async () => {
await fs.promises.writeFile(testFilePath, '# ', {encoding: 'utf8'}) await fs.promises.writeFile(testFilePath, '# ', {encoding: 'utf8'})
await markdownSummary.add(fixtures.text).write() await markdownSummary.addRaw(fixtures.text).write()
await assertSummary(`# ${fixtures.text}`) await assertSummary(`# ${fixtures.text}`)
}) })
it('overwrites text to summary file', async () => { it('overwrites text to summary file', async () => {
await fs.promises.writeFile(testFilePath, 'overwrite', {encoding: 'utf8'}) await fs.promises.writeFile(testFilePath, 'overwrite', {encoding: 'utf8'})
await markdownSummary.add(fixtures.text).write(true) await markdownSummary.addRaw(fixtures.text).write(true)
await assertSummary(fixtures.text) await assertSummary(fixtures.text)
}) })
it('appends text with EOL to summary file', async () => {
await fs.promises.writeFile(testFilePath, '# ', {encoding: 'utf8'})
await markdownSummary.addRaw(fixtures.text, true).write()
await assertSummary(`# ${fixtures.text}${os.EOL}`)
})
it('chains appends text to summary file', async () => { it('chains appends text to summary file', async () => {
await fs.promises.writeFile(testFilePath, '', {encoding: 'utf8'}) await fs.promises.writeFile(testFilePath, '', {encoding: 'utf8'})
await markdownSummary await markdownSummary
.add(fixtures.text) .addRaw(fixtures.text)
.add(fixtures.text) .addRaw(fixtures.text)
.add(fixtures.text) .addRaw(fixtures.text)
.write() .write()
await assertSummary([fixtures.text, fixtures.text, fixtures.text].join('')) await assertSummary([fixtures.text, fixtures.text, fixtures.text].join(''))
}) })
it('empties buffer after write', async () => { it('empties buffer after write', async () => {
await fs.promises.writeFile(testFilePath, '', {encoding: 'utf8'}) await fs.promises.writeFile(testFilePath, '', {encoding: 'utf8'})
await markdownSummary.add(fixtures.text).write() await markdownSummary.addRaw(fixtures.text).write()
await assertSummary(fixtures.text) await assertSummary(fixtures.text)
expect(markdownSummary.isEmptyBuffer()).toBe(true) expect(markdownSummary.isEmptyBuffer()).toBe(true)
}) })
it('returns summary buffer as string', () => { it('returns summary buffer as string', () => {
markdownSummary.add(fixtures.text) markdownSummary.addRaw(fixtures.text)
expect(markdownSummary.stringify()).toEqual(fixtures.text) expect(markdownSummary.stringify()).toEqual(fixtures.text)
}) })
it('return correct values for isEmptyBuffer', () => { it('return correct values for isEmptyBuffer', () => {
markdownSummary.add(fixtures.text) markdownSummary.addRaw(fixtures.text)
expect(markdownSummary.isEmptyBuffer()).toBe(false) expect(markdownSummary.isEmptyBuffer()).toBe(false)
markdownSummary.emptyBuffer() markdownSummary.emptyBuffer()
@ -147,7 +153,7 @@ describe('@actions/core/src/markdown-summary', () => {
it('adds EOL', async () => { it('adds EOL', async () => {
await markdownSummary await markdownSummary
.add(fixtures.text) .addRaw(fixtures.text)
.addEOL() .addEOL()
.write() .write()
await assertSummary(fixtures.text + os.EOL) await assertSummary(fixtures.text + os.EOL)

View File

@ -183,12 +183,13 @@ class MarkdownSummary {
* Adds raw text to the summary buffer * Adds raw text to the summary buffer
* *
* @param {string} text content to add * @param {string} text content to add
* @param {boolean} [addEOL=false] (optional) append an EOL to the raw text (default: false)
* *
* @returns {MarkdownSummary} markdown summary instance * @returns {MarkdownSummary} markdown summary instance
*/ */
add(text: string): MarkdownSummary { addRaw(text: string, addEOL = false): MarkdownSummary {
this._buffer += text this._buffer += text
return this return addEOL ? this.addEOL() : this
} }
/** /**
@ -197,7 +198,7 @@ class MarkdownSummary {
* @returns {MarkdownSummary} markdown summary instance * @returns {MarkdownSummary} markdown summary instance
*/ */
addEOL(): MarkdownSummary { addEOL(): MarkdownSummary {
return this.add(EOL) return this.addRaw(EOL)
} }
/** /**
@ -213,14 +214,14 @@ class MarkdownSummary {
...(lang && {lang}) ...(lang && {lang})
} }
const element = this.wrap('pre', this.wrap('code', code), attrs) const element = this.wrap('pre', this.wrap('code', code), attrs)
return this.add(element).addEOL() return this.addRaw(element).addEOL()
} }
/** /**
* Adds an HTML list to the summary buffer * Adds an HTML list to the summary buffer
* *
* @param {string[]} items list of items to render * @param {string[]} items list of items to render
* @param {boolean} [ordered=false] if the rendered list should be ordered or not (default: false) * @param {boolean} [ordered=false] (optional) if the rendered list should be ordered or not (default: false)
* *
* @returns {MarkdownSummary} markdown summary instance * @returns {MarkdownSummary} markdown summary instance
*/ */
@ -228,7 +229,7 @@ class MarkdownSummary {
const tag = ordered ? 'ol' : 'ul' const tag = ordered ? 'ol' : 'ul'
const listItems = items.map(item => this.wrap('li', item)).join('') const listItems = items.map(item => this.wrap('li', item)).join('')
const element = this.wrap(tag, listItems) const element = this.wrap(tag, listItems)
return this.add(element).addEOL() return this.addRaw(element).addEOL()
} }
/** /**
@ -263,7 +264,7 @@ class MarkdownSummary {
.join('') .join('')
const element = this.wrap('table', tableBody) const element = this.wrap('table', tableBody)
return this.add(element).addEOL() return this.addRaw(element).addEOL()
} }
/** /**
@ -276,7 +277,7 @@ class MarkdownSummary {
*/ */
addDetails(label: string, content: string): MarkdownSummary { addDetails(label: string, content: string): MarkdownSummary {
const element = this.wrap('details', this.wrap('summary', label) + content) const element = this.wrap('details', this.wrap('summary', label) + content)
return this.add(element).addEOL() return this.addRaw(element).addEOL()
} }
/** /**
@ -284,7 +285,7 @@ class MarkdownSummary {
* *
* @param {string} src path to the image you to embed * @param {string} src path to the image you to embed
* @param {string} alt text description of the image * @param {string} alt text description of the image
* @param {SummaryImageOptions} options addition image attributes * @param {SummaryImageOptions} options (optional) addition image attributes
* *
* @returns {MarkdownSummary} markdown summary instance * @returns {MarkdownSummary} markdown summary instance
*/ */
@ -300,7 +301,7 @@ class MarkdownSummary {
} }
const element = this.wrap('img', null, {src, alt, ...attrs}) const element = this.wrap('img', null, {src, alt, ...attrs})
return this.add(element).addEOL() return this.addRaw(element).addEOL()
} }
/** /**
@ -317,7 +318,7 @@ class MarkdownSummary {
? tag ? tag
: 'h1' : 'h1'
const element = this.wrap(allowedTag, text) const element = this.wrap(allowedTag, text)
return this.add(element).addEOL() return this.addRaw(element).addEOL()
} }
/** /**
@ -327,7 +328,7 @@ class MarkdownSummary {
*/ */
addSeparator(): MarkdownSummary { addSeparator(): MarkdownSummary {
const element = this.wrap('hr', null) const element = this.wrap('hr', null)
return this.add(element).addEOL() return this.addRaw(element).addEOL()
} }
/** /**
@ -337,7 +338,7 @@ class MarkdownSummary {
*/ */
addBreak(): MarkdownSummary { addBreak(): MarkdownSummary {
const element = this.wrap('br', null) const element = this.wrap('br', null)
return this.add(element).addEOL() return this.addRaw(element).addEOL()
} }
/** /**
@ -353,7 +354,7 @@ class MarkdownSummary {
...(cite && {cite}) ...(cite && {cite})
} }
const element = this.wrap('blockquote', text, attrs) const element = this.wrap('blockquote', text, attrs)
return this.add(element).addEOL() return this.addRaw(element).addEOL()
} }
/** /**
@ -366,7 +367,7 @@ class MarkdownSummary {
*/ */
addLink(text: string, href: string): MarkdownSummary { addLink(text: string, href: string): MarkdownSummary {
const element = this.wrap('a', text, {href}) const element = this.wrap('a', text, {href})
return this.add(element).addEOL() return this.addRaw(element).addEOL()
} }
} }