nitmws / odrl-wprofile-evaltest1

ODRL - with Profile - evaluator for test purposes
3 stars 1 forks source link

A relative path leading to a runtime error in node version v21.7.1 #1

Open ksridhar opened 7 months ago

ksridhar commented 7 months ago

At https://github.com/nitmws/odrl-wprofile-evaltest1/blob/09210763c3f86eec05b650f3e36aeca8a235cef0/services/configs.js#L7

$ node --version
v21.7.1
$ node main.js 
Error: ENOENT: no such file or directory, open '/home/sridhar/github/odrl-wprofile-evaltest1/services./../testdata/testconfig.yml'
    at Object.readFileSync (node:fs:455:20)
    at Object.loadTestconfig (/home/sridhar/github/odrl-wprofile-evaltest1/services/configs.js:7:47)
    at Object.doTheTest (/home/sridhar/github/odrl-wprofile-evaltest1/evaltests/test.js:25:13)
    at Object.<anonymous> (/home/sridhar/github/odrl-wprofile-evaltest1/main.js:13:7)
    at Module._compile (node:internal/modules/cjs/loader:1368:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1426:10)
    at Module.load (node:internal/modules/cjs/loader:1205:32)
    at Module._load (node:internal/modules/cjs/loader:1021:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:142:12)
    at node:internal/main/run_main_module:28:49 {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/home/sridhar/github/odrl-wprofile-evaltest1/services./../testdata/testconfig.yml'
}
/home/sridhar/github/odrl-wprofile-evaltest1/evaltests/test.js:29
    if (!configs.testconfig[caseName]){
                           ^

TypeError: Cannot read properties of undefined (reading 'case_imex12-1')
    at Object.doTheTest (/home/sridhar/github/odrl-wprofile-evaltest1/evaltests/test.js:29:28)
    at Object.<anonymous> (/home/sridhar/github/odrl-wprofile-evaltest1/main.js:13:7)
    at Module._compile (node:internal/modules/cjs/loader:1368:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1426:10)
    at Module.load (node:internal/modules/cjs/loader:1205:32)
    at Module._load (node:internal/modules/cjs/loader:1021:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:142:12)
    at node:internal/main/run_main_module:28:49

Node.js v21.7.1

It works with '/../testdata/testconfig.yml' instead of './../testdata/testconfig.yml'

spetrac commented 2 months ago

Should be fixed by using the join method of the node:path module.

"use strict"
let yaml = require("js-yaml")
let fs = require("fs")
let path = require("path")

function loadTestconfig() {
    try {
        let filePath = path.join(__dirname, './../testdata/testconfig.yml');
        exports.testconfig = yaml.safeLoad(fs.readFileSync(filePath, 'utf8'));
    } catch (e) {
        console.log(e);
    }
    Object.freeze(exports.testconfig);
}
exports.loadTestconfig = loadTestconfig;