File: normalize-manifest.js

package info (click to toggle)
node-yarnpkg 1.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 92,980 kB
  • sloc: sh: 323; makefile: 19
file content (185 lines) | stat: -rw-r--r-- 5,442 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* @flow */
/* eslint max-len: 0 */

import normalizeManifest from '../src/util/normalize-manifest/index.js';
import {BufferReporter} from '../src/reporters/index.js';
import Config from '../src/config.js';
import map from '../src/util/map.js';
import * as util from '../src/util/normalize-manifest/util.js';
import * as fs from '../src/util/fs.js';

const nativeFs = require('fs');
const path = require('path');

jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000;

const fixturesLoc = path.join(__dirname, 'fixtures', 'normalize-manifest');

async function _compareManifests(name, loc, {globalFolder} = {}): Promise<void> {
  const reporter = new BufferReporter();
  const config = await Config.create({cwd: loc, globalFolder}, reporter);

  let actual = await fs.readJson(path.join(loc, 'actual.json'));
  const expected = await fs.readJson(path.join(loc, 'expected.json'));

  let isRoot = actual._root;
  if (isRoot == null) {
    isRoot = true;
  } else {
    delete actual._root;
  }

  const error = expected._error;
  if (error) {
    delete expected._error;
  }

  try {
    actual = await normalizeManifest(actual, loc, config, isRoot);
  } catch (err) {
    if (error && err.message === error) {
      return;
    } else {
      throw err;
    }
  }

  if (error) {
    throw new Error(`Expected to throw error: ${error}`);
  }

  expect(map(actual)).toEqual(expand(expected));
  expect(
    reporter.getBuffer().filter(d => d.type === 'warning').map(d => String(d.data).replace(/\\/g, '/')),
  ).toMatchSnapshot(name);
}

for (const name of nativeFs.readdirSync(fixturesLoc)) {
  if (name[0] === '.') {
    continue;
  }

  const loc = path.join(fixturesLoc, name);

  test(name, () => _compareManifests(name, loc));
}

const globalFolder = path.join(__dirname, 'fixtures', 'normalize-manifest', 'name');

test('license warnings should not be thrown with global commands', () =>
  _compareManifests('license warnings should not be thrown with global commands', globalFolder, {globalFolder}));

test('util.stringifyPerson', () => {
  expect(util.stringifyPerson({name: 'Sebastian McKenzie'})).toEqual('Sebastian McKenzie');
  expect(
    util.stringifyPerson({
      name: 'Sebastian McKenzie',
      email: 'sebmck@gmail.com',
    }),
  ).toEqual('Sebastian McKenzie <sebmck@gmail.com>');
  expect(util.stringifyPerson({email: 'sebmck@gmail.com'})).toEqual('<sebmck@gmail.com>');
  expect(
    util.stringifyPerson({
      name: 'Sebastian McKenzie',
      email: 'sebmck@gmail.com',
      url: 'https://sebmck.com',
    }),
  ).toEqual('Sebastian McKenzie <sebmck@gmail.com> (https://sebmck.com)');
});

test('util.parsePerson', () => {
  expect(util.parsePerson({})).toEqual({});
  expect(util.parsePerson('Sebastian McKenzie')).toEqual({
    name: 'Sebastian McKenzie',
  });
  expect(util.parsePerson(' <sebmck@gmail.com>')).toEqual({
    email: 'sebmck@gmail.com',
  });
  expect(util.parsePerson('Sebastian McKenzie <sebmck@gmail.com>')).toEqual({
    name: 'Sebastian McKenzie',
    email: 'sebmck@gmail.com',
  });
  expect(util.parsePerson('Sebastian McKenzie <sebmck@gmail.com> (https://sebmck.com)')).toEqual({
    name: 'Sebastian McKenzie',
    email: 'sebmck@gmail.com',
    url: 'https://sebmck.com',
  });
});

test('util.extractDescription', () => {
  expect(util.extractDescription('# header\n\ndescription here')).toEqual('description here');
  expect(util.extractDescription('# header\ndescription here')).toEqual('description here');
  expect(util.extractDescription('# header\ndescription here\nfoobar')).toEqual('description here foobar');
  expect(util.extractDescription('# header\ndescription here\n\nfoobar')).toEqual('description here');
  expect(util.extractDescription('')).toEqual(undefined);
  expect(util.extractDescription(null)).toEqual(undefined);
  expect(util.extractDescription(undefined)).toEqual(undefined);
});

test('util.extractRepositoryUrl', () => {
  expect(util.extractRepositoryUrl('https://github.com/yarnpkg/yarn.git')).toEqual(
    'https://github.com/yarnpkg/yarn.git',
  );
  expect(
    util.extractRepositoryUrl({
      type: 'git',
      url: 'https://github.com/yarnpkg/yarn.git',
    }),
  ).toEqual('https://github.com/yarnpkg/yarn.git');
});

// fill out expected and normalize paths
function expand<T: Object>(expected: T): T {
  if (expected.man && Array.isArray(expected.man)) {
    expected = {...expected, man: normalizePaths(expected.man)};
  }

  if (expected.bin && typeof expected.bin === 'object') {
    expected = {...expected, bin: normalizePathDict(expected.bin)};
  }

  return expected;
}

function normalize(input: string): string {
  return path.normalize(input);
}

function normalizePath<T>(path: T): ?string {
  if (typeof path === 'string') {
    return normalize(path);
  } else {
    return null;
  }
}

function normalizePaths(paths: mixed): ?(string[]) {
  if (Array.isArray(paths)) {
    return paths.map(p => {
      if (typeof p !== 'string') {
        throw new Error(`Expected string in paths, got ${JSON.stringify(paths)}`);
      }
      return normalize(p);
    });
  } else {
    return null;
  }
}

function normalizePathDict(paths: mixed): ?{[key: string]: mixed} {
  const out = {};

  if (!paths || typeof paths !== 'object') {
    return null;
  }

  for (const prop in paths) {
    if (typeof paths[prop] === 'string') {
      out[prop] = normalizePath(paths[prop]);
    } else {
      out[prop] = paths[prop];
    }
  }

  return out;
}