|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: Copyright (c) 2022-2026 Objectionary.com |
| 3 | + * SPDX-License-Identifier: MIT |
| 4 | + */ |
| 5 | + |
| 6 | +const assert = require('assert'); |
| 7 | +const fs = require('fs'); |
| 8 | +const path = require('path'); |
| 9 | +const {execSync} = require('child_process'); |
| 10 | +const {runSync, parserVersion, homeTag, weAreOnline} = require('../helpers'); |
| 11 | + |
| 12 | +const eo = '# sample\n[] > simple\n'; |
| 13 | + |
| 14 | +function setup(name, content = eo) { |
| 15 | + const home = path.resolve('temp/test-normalize', name); |
| 16 | + const source = path.resolve(home, 'src'); |
| 17 | + const target = path.resolve(home, 'target'); |
| 18 | + fs.rmSync(home, {recursive: true, force: true}); |
| 19 | + fs.mkdirSync(source, {recursive: true}); |
| 20 | + fs.mkdirSync(target, {recursive: true}); |
| 21 | + fs.writeFileSync(path.resolve(source, 'simple.eo'), content); |
| 22 | + runSync([ |
| 23 | + 'normalize', |
| 24 | + '--verbose', |
| 25 | + `--parser=${parserVersion}`, |
| 26 | + `--home-tag=${homeTag}`, |
| 27 | + '-s', source, |
| 28 | + '-t', target, |
| 29 | + ]); |
| 30 | + return {home, source, target}; |
| 31 | +} |
| 32 | + |
| 33 | +describe('normalize', () => { |
| 34 | + before(weAreOnline); |
| 35 | + before(() => { |
| 36 | + try { |
| 37 | + execSync('phino --version', {stdio: 'pipe'}); |
| 38 | + } catch (e) { |
| 39 | + throw new Error( |
| 40 | + 'phino is required to run normalize tests, see https://github.com/objectionary/phino', |
| 41 | + {cause: e} |
| 42 | + ); |
| 43 | + } |
| 44 | + }); |
| 45 | + it('normalizes EO files and saves originals in before-normalize/', done => { |
| 46 | + const {source, target} = setup('simple'); |
| 47 | + assert( |
| 48 | + fs.existsSync(path.resolve(target, 'before-normalize/simple.eo')), |
| 49 | + 'Original .eo file should be saved in before-normalize/' |
| 50 | + ); |
| 51 | + assert( |
| 52 | + fs.existsSync(path.resolve(source, 'simple.eo')), |
| 53 | + 'Normalized .eo file should exist in sources directory' |
| 54 | + ); |
| 55 | + done(); |
| 56 | + }); |
| 57 | + it('normalized output matches expected content', done => { |
| 58 | + const {source} = setup('content'); |
| 59 | + const actual = fs.readFileSync(path.resolve(source, 'simple.eo'), 'utf8'); |
| 60 | + const expected = '# No comments.\n[] > simple\n'; |
| 61 | + assert.strictEqual( |
| 62 | + actual, expected, |
| 63 | + `Normalized output must equal expected.\nExpected:\n${expected}\nActual:\n${actual}` |
| 64 | + ); |
| 65 | + done(); |
| 66 | + }); |
| 67 | + it('backup matches original input and all pipeline files are produced', done => { |
| 68 | + const {source, target} = setup('pipeline'); |
| 69 | + const backup = fs.readFileSync( |
| 70 | + path.resolve(target, 'before-normalize/simple.eo'), 'utf8' |
| 71 | + ); |
| 72 | + assert.strictEqual( |
| 73 | + backup, eo, |
| 74 | + 'Backup in before-normalize/ must exactly match the original input' |
| 75 | + ); |
| 76 | + const xmir = path.resolve(target, 'xmir-normalized/simple.xmir'); |
| 77 | + assert(fs.existsSync(xmir), 'Normalized XMIR file must exist'); |
| 78 | + assert(fs.readFileSync(xmir).length > 0, 'Normalized XMIR file must not be empty'); |
| 79 | + const normalized = fs.readFileSync(path.resolve(source, 'simple.eo'), 'utf8'); |
| 80 | + assert(normalized.length > 0, 'Normalized .eo file must not be empty'); |
| 81 | + done(); |
| 82 | + }); |
| 83 | +}); |
0 commit comments