|
| 1 | +import { describe, it } from 'node:test' |
| 2 | +import assert from 'node:assert/strict' |
| 3 | +import { formatDockerfileContents } from './node.js' |
| 4 | + |
| 5 | +const defaultOptions = { |
| 6 | + indent: 4, |
| 7 | + trailingNewline: true, |
| 8 | + spaceRedirects: false, |
| 9 | +} |
| 10 | + |
| 11 | +describe('formatDockerfileContents', () => { |
| 12 | + it('formats a basic Dockerfile', async () => { |
| 13 | + const input = `from alpine |
| 14 | +run echo hello |
| 15 | +`.trim() |
| 16 | + |
| 17 | + const result = await formatDockerfileContents(input, defaultOptions) |
| 18 | + assert.equal(result, 'FROM alpine\nRUN echo hello\n') |
| 19 | + }) |
| 20 | + |
| 21 | + it('formats CMD JSON form with spaces', async () => { |
| 22 | + const input = `FROM alpine |
| 23 | +CMD ["ls","-la"] |
| 24 | +`.trim() |
| 25 | + |
| 26 | + const result = await formatDockerfileContents(input, defaultOptions) |
| 27 | + assert.equal(result, 'FROM alpine\nCMD ["ls", "-la"]\n') |
| 28 | + }) |
| 29 | + |
| 30 | + it('formats RUN JSON form with spaces', async () => { |
| 31 | + const input = `FROM alpine |
| 32 | +RUN ["echo","hello"] |
| 33 | +`.trim() |
| 34 | + |
| 35 | + const result = await formatDockerfileContents(input, defaultOptions) |
| 36 | + assert.equal(result, 'FROM alpine\nRUN ["echo", "hello"]\n') |
| 37 | + }) |
| 38 | + |
| 39 | + it('handles the issue #25 reproduction case', async () => { |
| 40 | + const input = ` |
| 41 | +FROM nginx |
| 42 | +WORKDIR /app |
| 43 | +ARG PROJECT_DIR=/ |
| 44 | +ARG NGINX_CONF=nginx.conf |
| 45 | +COPY $NGINX_CONF /etc/nginx/conf.d/nginx.conf |
| 46 | +COPY $PROJECT_DIR /app |
| 47 | +CMD mkdir --parents /var/log/nginx && nginx -g "daemon off;" |
| 48 | +`.trim() |
| 49 | + |
| 50 | + const result = await formatDockerfileContents(input, { |
| 51 | + indent: 4, |
| 52 | + spaceRedirects: false, |
| 53 | + trailingNewline: true, |
| 54 | + }) |
| 55 | + |
| 56 | + assert.ok(result.includes('FROM nginx')) |
| 57 | + assert.ok(result.includes('WORKDIR /app')) |
| 58 | + assert.ok(result.endsWith('\n')) |
| 59 | + }) |
| 60 | + |
| 61 | + it('respects trailingNewline: false', async () => { |
| 62 | + const input = 'FROM alpine' |
| 63 | + const result = await formatDockerfileContents(input, { |
| 64 | + ...defaultOptions, |
| 65 | + trailingNewline: false, |
| 66 | + }) |
| 67 | + assert.ok(!result.endsWith('\n')) |
| 68 | + }) |
| 69 | + |
| 70 | + it('respects indent option', async () => { |
| 71 | + const input = `FROM alpine |
| 72 | +RUN echo a \\ |
| 73 | + && echo b |
| 74 | +`.trim() |
| 75 | + |
| 76 | + const result = await formatDockerfileContents(input, { |
| 77 | + ...defaultOptions, |
| 78 | + indent: 2, |
| 79 | + }) |
| 80 | + assert.ok(result.includes(' && echo b')) |
| 81 | + }) |
| 82 | +}) |
0 commit comments