From 9668ee6ccaaa0555b3d09b31f1153e0624de5bb4 Mon Sep 17 00:00:00 2001 From: Evgeny Metelkin Date: Fri, 10 Jan 2025 13:59:17 +0200 Subject: [PATCH] support absolute paths for dist --- package.json | 2 +- src/builder/index.js | 18 +++-- test/filepaths/main.js | 148 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 162 insertions(+), 6 deletions(-) create mode 100644 test/filepaths/main.js diff --git a/package.json b/package.json index 358a66a9..a85975eb 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "Programming platform for Quantitative Systems Pharmacology modeling in NodeJS", "main": "src/index.js", "scripts": { - "test:dev": "mocha test/time-switcher/bind-error --config=./test/.mocharc.json", + "test:dev": "mocha test/filepaths --config=./test/.mocharc.json", "test": "mocha test --config=./test/.mocharc.json", "jsdoc": "jsdoc -r -c .jsdoc.json --readme api-references.md -d docs/dev src", "test:cov": "nyc --reporter=lcov npm run test", diff --git a/src/builder/index.js b/src/builder/index.js index 7d22c366..85b66a16 100644 --- a/src/builder/index.js +++ b/src/builder/index.js @@ -83,10 +83,16 @@ class Builder { Object.assign(this, declaration); // all relative or absolute depending on coreDirname - this._coreDirname = coreDirname; // already normalized - this._distDirname = path.join(this._coreDirname, declaration.options.distDir); // TODO: HOW TO MAKE relative from absolute - this._metaDirname = path.join(this._coreDirname, declaration.options.metaDir); - this._logPath = path.join(this._coreDirname, declaration.options.logPath); + this._coreDirname = coreDirname; // relative to the shell or absolute + this._distDirname = path.isAbsolute(declaration.options.distDir) + ? declaration.options.distDir // absolute path + : path.join(this._coreDirname, declaration.options.distDir); // relative to the shell + this._metaDirname = path.isAbsolute(declaration.options.metaDir) + ? declaration.options.metaDir // absolute path + : path.join(this._coreDirname, declaration.options.metaDir); // relative to the shell + this._logPath = path.isAbsolute(declaration.options.logPath) + ? declaration.options.logPath // absolute path + : path.join(this._coreDirname, declaration.options.logPath); // relative to the shell logger.info(`Builder initialized for the platform "${this.id}"`); @@ -136,7 +142,9 @@ class Builder { run() { // 1. Parsing let ms = new ModuleSystem(this.logger, this.fileReadHandler); - let absFilename = path.join(this._coreDirname, this.importModule.source); + let absFilename = path.isAbsolute(this.importModule.source) + ? this.importModule.source // absolute path + : path.join(this._coreDirname, this.importModule.source); // relative to the shell ms.addModuleDeep(absFilename, this.importModule.type, this.importModule); // 2. Modules integration diff --git a/test/filepaths/main.js b/test/filepaths/main.js new file mode 100644 index 00000000..d00f215f --- /dev/null +++ b/test/filepaths/main.js @@ -0,0 +1,148 @@ +/* global describe, it */ + +// check calculated paths + +const path = require('path'); +const { expect } = require('chai'); +const { Builder } = require('../../src/builder'); + +describe('Check Builder paths with defailt options', () => { + it('Empty coreDirName', () => { + const b = new Builder(); + + expect(b._coreDirname).to.be.equal('.'); + expect(b._distDirname).to.be.equal('dist'); + expect(b._metaDirname).to.be.equal('meta'); + expect(b._logPath).to.be.equal('build.log'); + }); + + it('Relative coreDirName linux', () => { + const b = new Builder({}, 'test/dir'); + + expect(path.normalize(b._coreDirname)).to.be.equal(path.normalize('test/dir')); + expect(path.normalize(b._distDirname)).to.be.equal(path.normalize('test/dir/dist')); + expect(path.normalize(b._metaDirname)).to.be.equal(path.normalize('test/dir/meta')); + expect(path.normalize(b._logPath)).to.be.equal(path.normalize('test/dir/build.log')); + }); + + it('Relative coreDirName windows', () => { + const b = new Builder({}, 'test\\dir'); + + expect(path.normalize(b._coreDirname)).to.be.equal(path.normalize('test/dir')); + expect(path.normalize(b._distDirname)).to.be.equal(path.normalize('test/dir/dist')); + expect(path.normalize(b._metaDirname)).to.be.equal(path.normalize('test/dir/meta')); + expect(path.normalize(b._logPath)).to.be.equal(path.normalize('test/dir/build.log')); + }); + + it('Absolute coreDirName linux', () => { + const b = new Builder({}, '/test/dir'); + + expect(path.normalize(b._coreDirname)).to.be.equal(path.normalize('/test/dir')); + expect(path.normalize(b._distDirname)).to.be.equal(path.normalize('/test/dir/dist')); + expect(path.normalize(b._metaDirname)).to.be.equal(path.normalize('/test/dir/meta')); + expect(path.normalize(b._logPath)).to.be.equal(path.normalize('/test/dir/build.log')); + }); + + it('Absolute coreDirName windows', () => { + const b = new Builder({}, 'C:\\test\\dir'); + + expect(path.normalize(b._coreDirname)).to.be.equal(path.normalize('C:/test/dir')); + expect(path.normalize(b._distDirname)).to.be.equal(path.normalize('C:/test/dir/dist')); + expect(path.normalize(b._metaDirname)).to.be.equal(path.normalize('C:/test/dir/meta')); + expect(path.normalize(b._logPath)).to.be.equal(path.normalize('C:/test/dir/build.log')); + }); +}); + +describe('Check Builder paths with relative dist dir', () => { + it('Empty coreDirName', () => { + const b = new Builder({options: {distDir: 'dist2', metaDir: 'meta2', logPath: 'build2.log'}}); + + expect(b._coreDirname).to.be.equal('.'); + expect(b._distDirname).to.be.equal('dist2'); + expect(b._metaDirname).to.be.equal('meta2'); + expect(b._logPath).to.be.equal('build2.log'); + }); + + it('Relative coreDirName linux', () => { + const b = new Builder({options: {distDir: 'dist2', metaDir: 'meta2', logPath: 'build2.log'}}, 'test/dir'); + + expect(path.normalize(b._coreDirname)).to.be.equal(path.normalize('test/dir')); + expect(path.normalize(b._distDirname)).to.be.equal(path.normalize('test/dir/dist2')); + expect(path.normalize(b._metaDirname)).to.be.equal(path.normalize('test/dir/meta2')); + expect(path.normalize(b._logPath)).to.be.equal(path.normalize('test/dir/build2.log')); + }); + + it('Relative coreDirName windows', () => { + const b = new Builder({options: {distDir: 'dist2', metaDir: 'meta2', logPath: 'build2.log'}}, 'test\\dir'); + + expect(path.normalize(b._coreDirname)).to.be.equal(path.normalize('test/dir')); + expect(path.normalize(b._distDirname)).to.be.equal(path.normalize('test/dir/dist2')); + expect(path.normalize(b._metaDirname)).to.be.equal(path.normalize('test/dir/meta2')); + expect(path.normalize(b._logPath)).to.be.equal(path.normalize('test/dir/build2.log')); + }); + + it('Absolute coreDirName linux', () => { + const b = new Builder({options: {distDir: 'dist2', metaDir: 'meta2', logPath: 'build2.log'}}, '/test/dir'); + + expect(path.normalize(b._coreDirname)).to.be.equal(path.normalize('/test/dir')); + expect(path.normalize(b._distDirname)).to.be.equal(path.normalize('/test/dir/dist2')); + expect(path.normalize(b._metaDirname)).to.be.equal(path.normalize('/test/dir/meta2')); + expect(path.normalize(b._logPath)).to.be.equal(path.normalize('/test/dir/build2.log')); + }); + + it('Absolute coreDirName windows', () => { + const b = new Builder({options: {distDir: 'dist2', metaDir: 'meta2', logPath: 'build2.log'}}, 'C:\\test\\dir'); + + expect(path.normalize(b._coreDirname)).to.be.equal(path.normalize('C:/test/dir')); + expect(path.normalize(b._distDirname)).to.be.equal(path.normalize('C:/test/dir/dist2')); + expect(path.normalize(b._metaDirname)).to.be.equal(path.normalize('C:/test/dir/meta2')); + expect(path.normalize(b._logPath)).to.be.equal(path.normalize('C:/test/dir/build2.log')); + }); +}); + +describe('Check Builder paths with absolute dist dir', () => { + it('Empty coreDirName', () => { + const b = new Builder({options: {distDir: '/some/dir/dist2', metaDir: '/some/dir/meta2', logPath: '/some/dir/build2.log'}}); + + expect(b._coreDirname).to.be.equal('.'); + expect(b._distDirname).to.be.equal('/some/dir/dist2'); + expect(b._metaDirname).to.be.equal('/some/dir/meta2'); + expect(b._logPath).to.be.equal('/some/dir/build2.log'); + }); + + it('Relative coreDirName linux', () => { + const b = new Builder({options: {distDir: '/some/dir/dist2', metaDir: '/some/dir/meta2', logPath: '/some/dir/build2.log'}}, 'test/dir'); + + expect(path.normalize(b._coreDirname)).to.be.equal(path.normalize('test/dir')); + expect(path.normalize(b._distDirname)).to.be.equal(path.normalize('/some/dir/dist2')); + expect(path.normalize(b._metaDirname)).to.be.equal(path.normalize('/some/dir/meta2')); + expect(path.normalize(b._logPath)).to.be.equal(path.normalize('/some/dir/build2.log')); + }); + + it('Relative coreDirName windows', () => { + const b = new Builder({options: {distDir: '/some/dir/dist2', metaDir: '/some/dir/meta2', logPath: '/some/dir/build2.log'}}, 'test\\dir'); + + expect(path.normalize(b._coreDirname)).to.be.equal(path.normalize('test/dir')); + expect(path.normalize(b._distDirname)).to.be.equal(path.normalize('/some/dir/dist2')); + expect(path.normalize(b._metaDirname)).to.be.equal(path.normalize('/some/dir/meta2')); + expect(path.normalize(b._logPath)).to.be.equal(path.normalize('/some/dir/build2.log')); + }); + + it('Absolute coreDirName linux', () => { + const b = new Builder({options: {distDir: '/some/dir/dist2', metaDir: '/some/dir/meta2', logPath: '/some/dir/build2.log'}}, '/test/dir'); + + expect(path.normalize(b._coreDirname)).to.be.equal(path.normalize('/test/dir')); + expect(path.normalize(b._distDirname)).to.be.equal(path.normalize('/some/dir/dist2')); + expect(path.normalize(b._metaDirname)).to.be.equal(path.normalize('/some/dir/meta2')); + expect(path.normalize(b._logPath)).to.be.equal(path.normalize('/some/dir/build2.log')); + }); + + it('Absolute coreDirName windows', () => { + const b = new Builder({options: {distDir: '/some/dir/dist2', metaDir: '/some/dir/meta2', logPath: '/some/dir/build2.log'}}, 'C:\\test\\dir'); + + expect(path.normalize(b._coreDirname)).to.be.equal(path.normalize('C:/test/dir')); + expect(path.normalize(b._distDirname)).to.be.equal(path.normalize('/some/dir/dist2')); + expect(path.normalize(b._metaDirname)).to.be.equal(path.normalize('/some/dir/meta2')); + expect(path.normalize(b._logPath)).to.be.equal(path.normalize('/some/dir/build2.log')); + }); +}); \ No newline at end of file