|
1 |
| -import {wait} from '../src/wait' |
2 |
| -import * as process from 'process' |
3 |
| -import * as cp from 'child_process' |
4 |
| -import * as path from 'path' |
| 1 | +import {getFileOwners, parseCodeOwnersContent} from '../src/main' |
5 | 2 |
|
6 |
| -test('throws invalid number', async () => { |
7 |
| - const input = parseInt('foo', 10) |
8 |
| - await expect(wait(input)).rejects.toThrow('milliseconds not a number') |
9 |
| -}) |
| 3 | +describe('parseCodeOwnersContent', () => { |
| 4 | + const tests = [ |
| 5 | + { |
| 6 | + name: 'single line', |
| 7 | + content: '/foo @nikclayton', |
| 8 | + want: [{path: '/foo', owners: ['@nikclayton']}] |
| 9 | + }, |
| 10 | + { |
| 11 | + name: 'comment', |
| 12 | + content: `# This is a comment |
| 13 | +/foo @nikclayton`, |
| 14 | + want: [{path: '/foo', owners: ['@nikclayton']}] |
| 15 | + }, |
| 16 | + { |
| 17 | + name: 'multiple owners', |
| 18 | + content: '/foo @bar @baz', |
| 19 | + want: [{path: '/foo', owners: ['@bar', '@baz']}] |
| 20 | + } |
| 21 | + ] |
10 | 22 |
|
11 |
| -test('wait 500 ms', async () => { |
12 |
| - const start = new Date() |
13 |
| - await wait(500) |
14 |
| - const end = new Date() |
15 |
| - var delta = Math.abs(end.getTime() - start.getTime()) |
16 |
| - expect(delta).toBeGreaterThan(450) |
| 23 | + for (const t of tests) { |
| 24 | + test(t.name, () => { |
| 25 | + const got = parseCodeOwnersContent(t.content) |
| 26 | + expect(got).toEqual(t.want) |
| 27 | + }) |
| 28 | + } |
17 | 29 | })
|
18 | 30 |
|
19 |
| -// shows how the runner will run a javascript action with env / stdout protocol |
20 |
| -test('test runs', () => { |
21 |
| - process.env['INPUT_MILLISECONDS'] = '500' |
22 |
| - const np = process.execPath |
23 |
| - const ip = path.join(__dirname, '..', 'lib', 'main.js') |
24 |
| - const options: cp.ExecFileSyncOptions = { |
25 |
| - env: process.env |
| 31 | +describe('getFileOwners', () => { |
| 32 | + const tests = [ |
| 33 | + { |
| 34 | + name: 'single owner', |
| 35 | + content: '/foo @nikclayton', |
| 36 | + filename: 'foo', |
| 37 | + want: ['nikclayton'] |
| 38 | + }, |
| 39 | + { |
| 40 | + name: 'multiple owners', |
| 41 | + content: '/foo @bar @baz', |
| 42 | + filename: 'foo', |
| 43 | + want: ['bar', 'baz'] |
| 44 | + }, |
| 45 | + { |
| 46 | + name: 'last entry wins', |
| 47 | + content: `/foo @bar @baz |
| 48 | +/foo @fred`, |
| 49 | + filename: 'foo', |
| 50 | + want: ['fred'] |
| 51 | + }, |
| 52 | + { |
| 53 | + name: 'file in any subdirectory matches', |
| 54 | + content: 'foo/ @bar', |
| 55 | + filename: '/foo/bar/baz', |
| 56 | + want: ['bar'] |
| 57 | + }, |
| 58 | + { |
| 59 | + name: 'only immediate children are tested (1)', |
| 60 | + content: 'foo/*.txt @bar', |
| 61 | + filename: 'foo/test.txt', |
| 62 | + want: ['bar'] |
| 63 | + }, |
| 64 | + { |
| 65 | + name: 'only immediate children are tested (2)', |
| 66 | + content: 'foo/*.txt @bar', |
| 67 | + filename: '/foo/bar/test.txt', |
| 68 | + want: [] |
| 69 | + }, |
| 70 | + { |
| 71 | + name: 'file with no owners', |
| 72 | + content: '/foo @bar', |
| 73 | + filename: 'not-in-codeowners', |
| 74 | + want: [] |
| 75 | + }, |
| 76 | + { |
| 77 | + name: '@ghost implies no owners', |
| 78 | + content: '/foo @ghost', |
| 79 | + filename: 'foo', |
| 80 | + want: [] |
| 81 | + } |
| 82 | + ] |
| 83 | + |
| 84 | + for (const t of tests) { |
| 85 | + test(t.name, () => { |
| 86 | + const codeOwners = parseCodeOwnersContent(t.content) |
| 87 | + const owners = getFileOwners(codeOwners, t.filename) |
| 88 | + expect(owners).toEqual(t.want) |
| 89 | + }) |
26 | 90 | }
|
27 |
| - console.log(cp.execFileSync(np, [ip], options).toString()) |
28 | 91 | })
|
0 commit comments