|
| 1 | +import * as core from '@actions/core' |
| 2 | +import {parseParams} from '../../src/functions/params' |
| 3 | + |
| 4 | +beforeEach(() => { |
| 5 | + jest.clearAllMocks() |
| 6 | + jest.spyOn(core, 'debug').mockImplementation(() => {}) |
| 7 | +}) |
| 8 | + |
| 9 | +test('it parses positional parameters', async () => { |
| 10 | + expect(parseParams('foo')).toHaveProperty('_', ['foo']) |
| 11 | +}) |
| 12 | + |
| 13 | +test('it parses arguments using the default settings of library', async () => { |
| 14 | + const parsed = parseParams('--foo bar --env.foo=bar') |
| 15 | + expect(parsed).toHaveProperty('foo', 'bar') |
| 16 | + expect(parsed).toHaveProperty('env', {foo: 'bar'}) |
| 17 | + expect(parsed).toHaveProperty('_', []) |
| 18 | +}) |
| 19 | + |
| 20 | +test('it works with empty string', async () => { |
| 21 | + expect(parseParams('')).toHaveProperty('_', []) |
| 22 | +}) |
| 23 | + |
| 24 | +test('it parses multiple positional parameters', async () => { |
| 25 | + expect(parseParams('foo bar baz')).toHaveProperty('_', ['foo', 'bar', 'baz']) |
| 26 | +}) |
| 27 | + |
| 28 | +test('it parses flags correctly', async () => { |
| 29 | + const parsed = parseParams('--foo --bar') |
| 30 | + expect(parsed).toHaveProperty('foo', true) |
| 31 | + expect(parsed).toHaveProperty('bar', true) |
| 32 | + expect(parsed).toHaveProperty('_', []) |
| 33 | +}) |
| 34 | + |
| 35 | +test('it parses numeric values correctly', async () => { |
| 36 | + const parsed = parseParams('--count 42') |
| 37 | + expect(parsed).toHaveProperty('count', 42) |
| 38 | + expect(parsed).toHaveProperty('_', []) |
| 39 | +}) |
| 40 | + |
| 41 | +test('it parses plain values', async () => { |
| 42 | + const parsed = parseParams('count 42') |
| 43 | + expect(parsed).toHaveProperty('_', ['count', 42]) |
| 44 | +}) |
| 45 | + |
| 46 | +test('it parses string values with comma separation', async () => { |
| 47 | + const parsed = parseParams('LOG_LEVEL=debug,CPU_CORES=4') |
| 48 | + expect(parsed).toHaveProperty('_', ['LOG_LEVEL=debug,CPU_CORES=4']) |
| 49 | +}) |
| 50 | + |
| 51 | +test('it parses boolean values correctly', async () => { |
| 52 | + const parsed = parseParams('--enabled=true --disabled false') |
| 53 | + expect(parsed).toHaveProperty('enabled', 'true') |
| 54 | + expect(parsed).toHaveProperty('disabled', 'false') |
| 55 | + expect(parsed).toHaveProperty('_', []) |
| 56 | +}) |
| 57 | + |
| 58 | +test('it parses nested objects correctly', async () => { |
| 59 | + const parsed = parseParams('--config.db.host=localhost --config.db.port=5432') |
| 60 | + expect(parsed).toHaveProperty('config', {db: {host: 'localhost', port: 5432}}) |
| 61 | + expect(parsed).toHaveProperty('_', []) |
| 62 | +}) |
| 63 | + |
| 64 | +test('it parses a real world example correctly', async () => { |
| 65 | + const parsed = parseParams( |
| 66 | + '--cpu=2 --memory=4G --env=development --port=8080 --name=my-app -q my-queue' |
| 67 | + ) |
| 68 | + expect(parsed).toHaveProperty('cpu', 2) |
| 69 | + expect(parsed).toHaveProperty('memory', '4G') |
| 70 | + expect(parsed).toHaveProperty('env', 'development') |
| 71 | + expect(parsed).toHaveProperty('port', 8080) |
| 72 | + expect(parsed).toHaveProperty('name', 'my-app') |
| 73 | + expect(parsed).toHaveProperty('q', 'my-queue') |
| 74 | + expect(parsed).toHaveProperty('_', []) |
| 75 | +}) |
0 commit comments