|
| 1 | +const LastComment = require('../../../lib/validators/lastComment') |
| 2 | +const Helper = require('../../../__fixtures__/unit/helper') |
| 3 | + |
| 4 | +test('validate returns correctly', async () => { |
| 5 | + const lastComment = new LastComment() |
| 6 | + |
| 7 | + const settings = { |
| 8 | + do: 'lastComment', |
| 9 | + must_exclude: { |
| 10 | + regex: 'exclude this' |
| 11 | + } |
| 12 | + } |
| 13 | + |
| 14 | + let results = await lastComment.processValidate(createMockContext(['exclude this']), settings) |
| 15 | + expect(results.status).toBe('fail') |
| 16 | + |
| 17 | + results = await lastComment.processValidate(createMockContext(['a', 'b']), settings) |
| 18 | + expect(results.status).toBe('pass') |
| 19 | +}) |
| 20 | + |
| 21 | +test('fail gracefully if invalid regex', async () => { |
| 22 | + const lastComment = new LastComment() |
| 23 | + |
| 24 | + const settings = { |
| 25 | + do: 'lastComment', |
| 26 | + must_exclude: { |
| 27 | + regex: '@#$@#$@#$' |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + const validation = await lastComment.processValidate(createMockContext('WIP'), settings) |
| 32 | + expect(validation.status).toBe('pass') |
| 33 | +}) |
| 34 | + |
| 35 | +test('description is correct', async () => { |
| 36 | + const lastComment = new LastComment() |
| 37 | + |
| 38 | + const settings = { |
| 39 | + do: 'lastComment', |
| 40 | + must_exclude: { |
| 41 | + regex: 'Work in Progress' |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + let validation = await lastComment.processValidate(createMockContext('Work in Progress'), settings) |
| 46 | + |
| 47 | + expect(validation.status).toBe('fail') |
| 48 | + expect(validation.validations[0].description).toBe('lastComment does not exclude "Work in Progress"') |
| 49 | + |
| 50 | + validation = await lastComment.processValidate(createMockContext('Just lastComment'), settings) |
| 51 | + expect(validation.validations[0].description).toBe("lastComment must exclude 'Work in Progress'") |
| 52 | +}) |
| 53 | + |
| 54 | +test('mergeable is true if must_include is the last comment', async () => { |
| 55 | + const lastComment = new LastComment() |
| 56 | + |
| 57 | + const settings = { |
| 58 | + do: 'lastComment', |
| 59 | + must_include: { |
| 60 | + regex: 'xyz' |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + let validation = await lastComment.processValidate(createMockContext(['abc', 'experimental', 'xyz']), settings) |
| 65 | + expect(validation.status).toBe('pass') |
| 66 | + |
| 67 | + validation = await lastComment.processValidate(createMockContext(['Some lastComment', 'xyz', '456']), settings) |
| 68 | + expect(validation.status).toBe('fail') |
| 69 | +}) |
| 70 | + |
| 71 | +test('mergeable is false if must_exclude is one of the lastComment', async () => { |
| 72 | + const lastComment = new LastComment() |
| 73 | + |
| 74 | + const settings = { |
| 75 | + do: 'lastComment', |
| 76 | + must_exclude: { |
| 77 | + regex: 'xyz' |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + let validation = await lastComment.processValidate(createMockContext(['abc', 'experimental', 'xyz']), settings) |
| 82 | + expect(validation.status).toBe('fail') |
| 83 | + |
| 84 | + validation = await lastComment.processValidate(createMockContext(['Some lastComment', 'xyz', '456']), settings) |
| 85 | + expect(validation.status).toBe('pass') |
| 86 | +}) |
| 87 | + |
| 88 | +test('complex Logic test', async () => { |
| 89 | + const lastComment = new LastComment() |
| 90 | + |
| 91 | + const settings = { |
| 92 | + do: 'lastComment', |
| 93 | + or: [{ |
| 94 | + and: [{ |
| 95 | + must_include: { |
| 96 | + regex: 'release note: yes', |
| 97 | + message: 'Please include release note: yes' |
| 98 | + } |
| 99 | + }, { |
| 100 | + must_include: { |
| 101 | + regex: 'lang\\/core|lang\\/c\\+\\+|lang\\/c#', |
| 102 | + message: 'Please include a language comment' |
| 103 | + } |
| 104 | + }] |
| 105 | + }, { |
| 106 | + must_include: { |
| 107 | + regex: 'release note: no', |
| 108 | + message: 'Please include release note: no' |
| 109 | + } |
| 110 | + }] |
| 111 | + } |
| 112 | + |
| 113 | + let validation = await lastComment.processValidate(createMockContext(['experimental', 'xyz', 'release note: no']), settings) |
| 114 | + expect(validation.status).toBe('pass') |
| 115 | + |
| 116 | + validation = await lastComment.processValidate(createMockContext(['123', '456', 'release note: yes']), settings) |
| 117 | + expect(validation.status).toBe('fail') |
| 118 | + expect(validation.validations[0].description).toBe('((Please include a language comment) ***OR*** Please include release note: no)') |
| 119 | + |
| 120 | + validation = await lastComment.processValidate(createMockContext(['456', 'lang/core']), settings) |
| 121 | + expect(validation.validations[0].description).toBe('((Please include release note: yes) ***OR*** Please include release note: no)') |
| 122 | +}) |
| 123 | + |
| 124 | +function createMockContext (comments) { |
| 125 | + return Helper.mockContext({ listComments: Array.isArray(comments) ? comments.map(comment => ({ body: comment })) : [{ body: comments }] }) |
| 126 | +} |
0 commit comments