v7
  1// Copyright 2021 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 { suite } from 'uvu'
 16import * as assert from 'uvu/assert'
 17import { $ } from '../build/index.js'
 18import { installDeps, parseDeps } from '../build/deps.js'
 19
 20const test = suite('deps')
 21
 22$.verbose = false
 23
 24test('installDeps() loader works via JS API', async () => {
 25  await installDeps({
 26    cpy: '9.0.1',
 27    'lodash-es': '4.17.21',
 28  })
 29  assert.instance((await import('cpy')).default, Function)
 30  assert.instance((await import('lodash-es')).pick, Function)
 31})
 32
 33test('installDeps() loader works via CLI', async () => {
 34  let out =
 35    await $`node build/cli.js --install <<< 'import _ from "lodash" /* @4.17.15 */; console.log(_.VERSION)'`
 36  assert.match(out.stdout, '4.17.15')
 37})
 38
 39test('parseDeps(): import or require', async () => {
 40  assert.equal(parseDeps(`import "foo"`), { foo: 'latest' })
 41  assert.equal(parseDeps(`import * as bar from "foo"`), { foo: 'latest' })
 42  assert.equal(parseDeps(`import('foo')`), { foo: 'latest' })
 43  assert.equal(parseDeps(`require('foo')`), { foo: 'latest' })
 44  assert.equal(parseDeps(`require('foo.js')`), { 'foo.js': 'latest' })
 45})
 46
 47test('parseDeps(): import with org and filename', async () => {
 48  assert.equal(parseDeps(`import "@foo/bar/file"`), { '@foo/bar': 'latest' })
 49})
 50
 51test('parseDeps(): import with version', async () => {
 52  assert.equal(parseDeps(`import "foo" // @2.x`), { foo: '2.x' })
 53  assert.equal(parseDeps(`import "foo" // @^7`), { foo: '^7' })
 54  assert.equal(parseDeps(`import "foo" /* @1.2.x */`), { foo: '1.2.x' })
 55})
 56
 57test('parseDeps(): multiline', () => {
 58  const contents = `
 59  require('a') // @1.0.0
 60  const b =require('b') /* @2.0.0 */
 61  const c = {
 62    c:require('c') /* @3.0.0 */, 
 63    d: await import('d') /* @4.0.0 */, 
 64    ...require('e') /* @5.0.0 */
 65  }
 66  const f = [...require('f') /* @6.0.0 */] 
 67  ;require('g'); // @7.0.0
 68  const h = 1 *require('h') // @8.0.0
 69  {require('i') /* @9.0.0 */}
 70  import 'j' // @10.0.0
 71
 72  import fs from 'fs'
 73  import path from 'path'
 74  import foo from "foo"
 75  import bar from "bar" /* @1.0.0 */
 76  import baz from "baz" //    @^2.0
 77
 78  const cpy = await import('cpy')
 79  const { pick } = require("lodash") //  @4.17.15
 80  `
 81
 82  assert.equal(parseDeps(contents), {
 83    a: '1.0.0',
 84    b: '2.0.0',
 85    c: '3.0.0',
 86    d: '4.0.0',
 87    e: '5.0.0',
 88    f: '6.0.0',
 89    g: '7.0.0',
 90    h: '8.0.0',
 91    i: '9.0.0',
 92    j: '10.0.0',
 93    foo: 'latest',
 94    bar: '1.0.0',
 95    baz: '^2.0',
 96    cpy: 'latest',
 97    lodash: '4.17.15',
 98  })
 99})
100
101test.run()