Skip to content

Commit

Permalink
test: test whether '--backgroundColor' works
Browse files Browse the repository at this point in the history
Adds some simple tests that check whether the backgroundColor option
works.

It looks like it doesn't work for PDFs. If we want to enable PDF
background colors, we need to set the puppeteer option
`printBackground: true`,
see https://pptr.dev/api/puppeteer.pdfoptions.printbackground
  • Loading branch information
aloisklink authored and MindaugasLaganeckas committed Aug 22, 2022
1 parent bbb90a5 commit d118c96
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .github/workflows/compile-mermaid.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ jobs:
# This will overwite any PNG files with the same name that have been created by run-tests.sh
$(npm bin)/convert-svg-to-png ${{env.INPUT_DATA}}/graph-with-br.mmd.svg
$(npm bin)/convert-svg-to-png ${{env.INPUT_DATA}}/graph-with-br.mmd-stdin.svg
# svg file is named `-svg` so it doesn't overwrite .png file
$(npm bin)/convert-svg-to-png ${{env.INPUT_DATA}}/flowchart1-red-background-svg.svg
- name: Upload diagrams for manual inspection
uses: actions/upload-artifact@v3
Expand Down
7 changes: 7 additions & 0 deletions run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ for i in $(ls $INPUT_DATA/*.md); do docker run --rm -v $(pwd):/data $IMAGETAG -i
# Test if the CLI actually works (PDF)
for i in $(ls $INPUT_DATA/*.mmd); do docker run --rm -v $(pwd):/data $IMAGETAG -i /data/$i -o /data/$i.pdf; done

# Test if passing background colors works
for format in "svg" "png"; do
# must have different names, since we convert .svg to .png for percy upload
outputFileName="/data/$INPUT_DATA/flowchart1-red-background-${format}.${format}"
docker run --rm -v $(pwd):/data $IMAGETAG -i /data/$INPUT_DATA/flowchart1.mmd --backgroundColor "red" -o "$outputFileName"
done

# Test if a diagram from STDIN can be understood
cat $INPUT_DATA/flowchart1.mmd | docker run --rm -i -v $(pwd):/data $IMAGETAG -o /data/$INPUT_DATA/flowchart1-stdin.png -w 800

Expand Down
7 changes: 7 additions & 0 deletions src-test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ describe("mermaid-cli", () => {
expectBytesAreFormat(await fs.readFile(file), 'svg')
}))
}, timeout)

test.concurrent.each(['svg', 'png', 'pdf'])('should set red background to %s', async (format) => {
await promisify(execFile)('node', [
'src/cli.js', '-i', 'test-positive/flowchart1.mmd', '-o', `test-output/flowchart1-red-background.${format}`,
'--backgroundColor', 'red'
])
}, timeout)
});

describe("NodeJS API (import ... from '@mermaid-js/mermaid-cli')", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ commander
.option('-H, --height [height]', 'Height of the page. Optional. Default: 600', /^\d+$/, '600')
.option('-i, --input <input>', 'Input mermaid file. Files ending in .md will be treated as Markdown and all charts (e.g. ```mermaid (...)```) will be extracted and generated. Required.')
.option('-o, --output [output]', 'Output file. It should be either md, svg, png or pdf. Optional. Default: input + ".svg"')
.option('-b, --backgroundColor [backgroundColor]', 'Background color. Example: transparent, red, \'#F0F0F0\'. Optional. Default: white')
.option('-b, --backgroundColor [backgroundColor]', 'Background color for pngs/svgs (not pdfs). Example: transparent, red, \'#F0F0F0\'. Optional. Default: white')
.option('-c, --configFile [configFile]', 'JSON configuration file for mermaid. Optional')
.option('-C, --cssFile [cssFile]', 'CSS file for the page. Optional')
.option('-s, --scale [scale]', 'Puppeteer scale factor, default 1. Optional')
Expand Down

0 comments on commit d118c96

Please sign in to comment.