diff --git a/.github/workflows/lint.js.yml b/.github/workflows/test.js.yml similarity index 95% rename from .github/workflows/lint.js.yml rename to .github/workflows/test.js.yml index 5542ba2..b41056e 100644 --- a/.github/workflows/lint.js.yml +++ b/.github/workflows/test.js.yml @@ -1,7 +1,7 @@ # This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node # For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions -name: Lint code +name: Lint and test on: push: @@ -29,3 +29,4 @@ jobs: node-version: ${{ matrix.node-version }} - run: npm ci - run: npm run lint + - run: npm test diff --git a/.gitignore b/.gitignore index 5bc352d..29d4947 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,7 @@ yarn-debug.log* yarn-error.log* .eslintcache + +# temp test data +/test/data/tmp/* +!/test/data/tmp/.keep diff --git a/package.json b/package.json index c689f5c..28ef4c1 100644 --- a/package.json +++ b/package.json @@ -17,8 +17,7 @@ "prepublishOnly": "npm run build", "start": "tsc --watch", "lint": "tsc && eslint --max-warnings=0 src/**/*.ts src/*.ts", - "ini:start": "npm run build && node dist/ini.js", - "ini:watch": "npx onchange src/ini.ts -- npm run ini:start" + "test": "npm run build && node test/test.js" }, "devDependencies": { "@types/js-yaml": "^4.0.3", diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..fa61496 --- /dev/null +++ b/src/index.ts @@ -0,0 +1 @@ +export * from './ini'; diff --git a/src/ini.ts b/src/ini.ts index b62ab1b..31e87eb 100644 --- a/src/ini.ts +++ b/src/ini.ts @@ -1,15 +1,10 @@ -import fs from 'fs'; -import path from 'path'; -import yaml from 'js-yaml'; import * as P from 'parsimmon'; import { Config as ConfigType, Constant, } from '@speedy-tuner/types'; -console.log('------- start --------'); - -class INI { +export class INI { space: P.Parser; expression: P.Parser; @@ -1155,27 +1150,3 @@ class INI { }; } -const versions = [ - // '202012', - // '202103', - '202109-dev', -]; - -versions.forEach((version) => { - const result = new INI( - fs.readFileSync(path.join(__dirname, `/../test/data/ini/${version}.ini`), 'utf8'), - ).parse(); - - fs.writeFileSync(path.join(__dirname, `/../test/data/yaml/${version}.yml`), yaml.dump(result)); - fs.writeFileSync(path.join(__dirname, `/../test/data/json/${version}.json`), JSON.stringify(result)); -}); - -// const result = new INI( -// fs.readFileSync(path.join(__dirname, `/../test/data/tunes/${versions[1]}.ini`), 'utf8'), -// ).parse(); -// console.dir( -// result.outputChannels, -// { depth: null, compact: false }, -// ); - -console.log('------- end --------'); diff --git a/test/data/tmp/.keep b/test/data/tmp/.keep new file mode 100644 index 0000000..e69de29 diff --git a/test/test.js b/test/test.js index 5bcad14..8e610bf 100644 --- a/test/test.js +++ b/test/test.js @@ -2,3 +2,54 @@ const fs = require('fs'); const path = require('path'); const assert = require('assert').strict; +const yaml = require('js-yaml'); +const crypto = require('crypto'); +const { INI } = require('../dist'); + +const VERSIONS = [ + '202012', + '202103', + '202109-dev', +]; + +const pathFor = (file) => path.join(__dirname, `/../test/data/${file}`); + +const hashReference = (version) => { + const md5Yaml = crypto.createHash('md5'); + const md5Json = crypto.createHash('md5'); + + md5Yaml.update(fs.readFileSync(pathFor(`yaml/${version}.yml`), 'utf8')); + md5Json.update(fs.readFileSync(pathFor(`json/${version}.json`), 'utf8')); + + return { + yamlOld: md5Yaml.digest('hex'), + jsonOld: md5Json.digest('hex'), + }; +}; + +VERSIONS.forEach((version) => { + const result = new INI( + fs.readFileSync(pathFor(`ini/${version}.ini`), 'utf8'), + ).parse(); + + const yamlContent = yaml.dump(result); + const jsonContent = JSON.stringify(result); + + const md5YamlNew = crypto.createHash('md5'); + const md5JsonNew = crypto.createHash('md5'); + + const yamlNew = md5YamlNew.update(yamlContent).digest('hex'); + const jsonNew = md5JsonNew.update(jsonContent).digest('hex'); + + const { yamlOld, jsonOld } = hashReference(version); + + // write temp files to disk so we can debug more easily + fs.writeFileSync(pathFor(`tmp/${version}.yml`), yamlContent); + fs.writeFileSync(pathFor(`tmp/${version}.json`), jsonContent); + + assert.equal(yamlNew, yamlOld); + assert.equal(jsonNew, jsonOld); + + fs.unlinkSync(pathFor(`tmp/${version}.yml`)); + fs.unlinkSync(pathFor(`tmp/${version}.json`)); +});