main
  1// Copyright 2022 Google LLC
  2//
  3// Licensed under the Apache License, Version 2.0 (the "License");
  4// you may not use this file except in compliance with the License.
  5// You may obtain a copy of the License at
  6//
  7//     https://www.apache.org/licenses/LICENSE-2.0
  8//
  9// Unless required by applicable law or agreed to in writing, software
 10// distributed under the License is distributed on an "AS IS" BASIS,
 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12// See the License for the specific language governing permissions and
 13// limitations under the License.
 14
 15import assert from 'node:assert'
 16import { test, describe } from 'node:test'
 17import {
 18  isString,
 19  isStringLiteral,
 20  identity,
 21  once,
 22  noop,
 23  parseDuration,
 24  parseBool,
 25  quote,
 26  quotePowerShell,
 27  randomId,
 28  // normalizeMultilinePieces,
 29  preferLocalBin,
 30  toCamelCase,
 31  getLast,
 32} from '../build/util.cjs'
 33
 34describe('util', () => {
 35  test('randomId()', () => {
 36    assert.ok(/^[a-z0-9]+$/.test(randomId()))
 37    assert.ok(
 38      new Set(Array.from({ length: 1000 }).map(() => randomId())).size === 1000
 39    )
 40  })
 41
 42  test('noop()', () => {
 43    assert.ok(noop() === undefined)
 44  })
 45
 46  test('once()', () => {
 47    const fn = once(identity)
 48    assert.equal(identity(1), 1)
 49    assert.equal(identity(2), 2)
 50    assert.equal(fn(1), 1)
 51    assert.equal(fn(2), 1)
 52  })
 53
 54  test('isString()', () => {
 55    assert.ok(isString('string'))
 56    assert.ok(!isString(1))
 57  })
 58
 59  test('isStringLiteral()', () => {
 60    const bar = 'baz'
 61    assert.ok(isStringLiteral``)
 62    assert.ok(isStringLiteral`foo`)
 63    assert.ok(isStringLiteral`foo ${bar}`)
 64
 65    assert.ok(!isStringLiteral(''))
 66    assert.ok(!isStringLiteral('foo'))
 67    assert.ok(!isStringLiteral(['foo']))
 68  })
 69
 70  test('quote()', () => {
 71    assert.ok(quote('string') === 'string')
 72    assert.ok(quote('') === `$''`)
 73    assert.ok(quote(`'\f\n\r\t\v\0`) === `$'\\'\\f\\n\\r\\t\\v\\0'`)
 74  })
 75
 76  test('quotePowerShell()', () => {
 77    assert.equal(quotePowerShell('string'), 'string')
 78    assert.equal(quotePowerShell(`'`), `''''`)
 79    assert.equal(quotePowerShell(''), `''`)
 80  })
 81
 82  test('duration parsing works', () => {
 83    assert.equal(parseDuration(0), 0)
 84    assert.equal(parseDuration(1000), 1000)
 85    assert.equal(parseDuration('100'), 100)
 86    assert.equal(parseDuration('2s'), 2000)
 87    assert.equal(parseDuration('500ms'), 500)
 88    assert.equal(parseDuration('2m'), 120000)
 89    assert.throws(() => parseDuration('f2ms'))
 90    assert.throws(() => parseDuration('2mss'))
 91    assert.throws(() => parseDuration(NaN))
 92    assert.throws(() => parseDuration(-1))
 93  })
 94
 95  // test('normalizeMultilinePieces()', () => {
 96  //   assert.equal(
 97  //     normalizeMultilinePieces([' a ', 'b    c    d', ' e']).join(','),
 98  //     ' a ,b c d, e'
 99  //   )
100  // })
101
102  test('preferLocalBin()', () => {
103    const env = {
104      PATH: '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin',
105    }
106    const _env = preferLocalBin(env, process.cwd())
107    assert.equal(
108      _env.PATH,
109      `${process.cwd()}/node_modules/.bin:${process.cwd()}:${env.PATH}`
110    )
111  })
112
113  test('toCamelCase()', () => {
114    assert.equal(toCamelCase('VERBOSE'), 'verbose')
115    assert.equal(toCamelCase('PREFER_LOCAL'), 'preferLocal')
116    assert.equal(toCamelCase('SOME_MORE_BIG_STR'), 'someMoreBigStr')
117    assert.equal(toCamelCase('kebab-input-str'), 'kebabInputStr')
118  })
119
120  test('parseBool()', () => {
121    assert.equal(parseBool('true'), true)
122    assert.equal(parseBool('false'), false)
123    assert.equal(parseBool('other'), 'other')
124  })
125
126  test('getLast()', () => {
127    assert.equal(getLast([1, 2, 3]), 3)
128    assert.equal(getLast([]), undefined)
129  })
130})