|
| 1 | +let fs = require('fs'); |
| 2 | +let path = require('path'); |
| 3 | +let util = require('util'); |
| 4 | +let yaml = require('./js-yaml.js'); |
| 5 | +let samples = require('./samples.json'); |
| 6 | + |
| 7 | +class LabelMatch { |
| 8 | + constructor (match, label) { |
| 9 | + this.match = match; |
| 10 | + this.label = label; |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +class FileError { |
| 15 | + constructor (file, actualLabels, expectedLabels) { |
| 16 | + this.file = file; |
| 17 | + this.actual = actualLabels; |
| 18 | + this.expected = expectedLabels; |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +class TextError { |
| 23 | + constructor (text, actualLabels, expectedLabels) { |
| 24 | + this.text = text; |
| 25 | + this.actual = actualLabels; |
| 26 | + this.expected = expectedLabels; |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +let labels = []; |
| 31 | + |
| 32 | +function labelsForFile(file) { |
| 33 | + let body = fs.readFileSync(file); |
| 34 | + return labelsForText(body) |
| 35 | +} |
| 36 | + |
| 37 | +function labelsForText(text) { |
| 38 | + let addLabels = new Set(); |
| 39 | + let body = text; |
| 40 | + for (const v of labels) { |
| 41 | + if (v.match.test(body)) { |
| 42 | + addLabels.add(v.label) |
| 43 | + } |
| 44 | + // reset regex state |
| 45 | + v.match.lastIndex = 0 |
| 46 | + } |
| 47 | + return addLabels; |
| 48 | +} |
| 49 | + |
| 50 | +try { |
| 51 | + let config = yaml.safeLoad(fs.readFileSync('../auto-labeler.yml', 'utf8')); |
| 52 | + |
| 53 | + if (config && config.labels && Object.keys(config.labels).length > 0) { |
| 54 | + for (const labelName in config.labels) { |
| 55 | + if (config.labels.hasOwnProperty(labelName)) { |
| 56 | + let matchAgainst = config.labels[labelName]; |
| 57 | + if (Array.isArray(matchAgainst)) { |
| 58 | + matchAgainst.forEach(regex => { |
| 59 | + // noinspection JSCheckFunctionSignatures |
| 60 | + labels.push(new LabelMatch(new RegExp(regex, 'g'), labelName)); |
| 61 | + }) |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + if (labels.length === 0) { |
| 68 | + // noinspection ExceptionCaughtLocallyJS |
| 69 | + throw new Error("Expected to parse config.labels, but failed.") |
| 70 | + } |
| 71 | + |
| 72 | + let fileErrors = []; |
| 73 | + samples.files.forEach(function(tester){ |
| 74 | + let file = path.normalize(path.join('..', '..', 'bin', tester.input)); |
| 75 | + let expectedLabels = new Set(tester.matches); |
| 76 | + let actualLabels = labelsForFile(file); |
| 77 | + let difference = new Set([...actualLabels].filter(x => !expectedLabels.has(x))); |
| 78 | + if (difference.size > 0) { |
| 79 | + fileErrors.push(new FileError(file, actualLabels, expectedLabels)); |
| 80 | + } |
| 81 | + }); |
| 82 | + |
| 83 | + let textErrors = []; |
| 84 | + samples.text.forEach(function(tester){ |
| 85 | + let expectedLabels = new Set(tester.matches); |
| 86 | + let actualLabels = labelsForText(tester.input); |
| 87 | + let difference = new Set([...actualLabels].filter(x => !expectedLabels.has(x))); |
| 88 | + if (difference.size > 0) { |
| 89 | + textErrors.push(new TextError(tester.input, actualLabels, expectedLabels)); |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + // These are separate (file vs text) in case we want to preview where these would fail in the file. not priority at the moment. |
| 94 | + if (fileErrors.length > 0) { |
| 95 | + console.warn('There were %d file tester errors', fileErrors.length); |
| 96 | + fileErrors.forEach(function(errs) { |
| 97 | + console.log("file: %j\n actual: %j\n expected: %j", errs.file, util.inspect(errs.actual), util.inspect(errs.expected)) |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + if (textErrors.length > 0) { |
| 102 | + console.warn('There were %d text tester errors', textErrors.length); |
| 103 | + textErrors.forEach(function(errs){ |
| 104 | + console.log("input: %j\n actual: %j\n expected: %j", errs.text, util.inspect(errs.actual), util.inspect(errs.expected)) |
| 105 | + }) |
| 106 | + } |
| 107 | + |
| 108 | + let totalErrors = fileErrors.length + textErrors.length; |
| 109 | + if (totalErrors === 0) { |
| 110 | + console.log('Success!'); |
| 111 | + } else { |
| 112 | + console.log('Failure: %d total errors', totalErrors); |
| 113 | + } |
| 114 | +} catch (e) { |
| 115 | + console.log(e); |
| 116 | +} |
| 117 | + |
0 commit comments