diff --git a/tests/json.test.js b/tests/json.test.js index 3a1e357..5533900 100644 --- a/tests/json.test.js +++ b/tests/json.test.js @@ -2,6 +2,7 @@ const core = require('@actions/core'); const getJSON = require('../utils/getJSON.js'); const checkInfo = require('../utils/checkInfo.js'); const checkRecords = require('../utils/checkRecords.js'); +const getFileName = require('../utils/getFileName.js'); const data = getJSON(process.env.FILES); function setupMessages() { @@ -48,3 +49,11 @@ test('Check if JSON file follows format', () => { core.setOutput('recordMessage', recordMessage); expect(passed).toBe(true); }); + +test('Check if JSON file matches the value name', () => { + const passed = getFileName(process.env.FILES, data); + let fileNameMessage = passed === true ? "File name matches target.RECORD_TYPE." : "File name does not match any target.RECORD_TYPE."; + + core.setOutput('fileNameMessage', fileNameMessage); + expect(passed).toBe(true); +}); diff --git a/utils/getFileName.js b/utils/getFileName.js new file mode 100644 index 0000000..c5ff20b --- /dev/null +++ b/utils/getFileName.js @@ -0,0 +1,17 @@ +const { stripExt } = require('./utils.js'); + +async function getFileName(file, data) { + // Extract the file name without extension + const fileNameWithoutExt = stripExt(file); + + // Validate if the file name matches any of the target.RECORD_TYPE values + const recordTypes = Object.keys(data.target); + if (!recordTypes.includes(fileNameWithoutExt.toUpperCase())) { + core.setOutput('infoMessage', `File name ${fileNameWithoutExt} does not match any target.RECORD_TYPE`); + return false; + } else { + return true; + } +} + +module.exports = getFileName; \ No newline at end of file diff --git a/utils/getJSON.js b/utils/getJSON.js index e76b1c1..e1fec48 100644 --- a/utils/getJSON.js +++ b/utils/getJSON.js @@ -15,16 +15,6 @@ function getJSON(file, filename) { const rawdata = fs.readFileSync(path); // Read the file const data = JSON.parse(rawdata); // Parse it - // Extract the file name without extension - const fileNameWithoutExt = stripExt(file); - - // Validate if the file name matches any of the target.RECORD_TYPE values - const recordTypes = Object.keys(data.target); - if (!recordTypes.includes(fileNameWithoutExt.toUpperCase())) { - core.setOutput('infoMessage', `File name ${fileNameWithoutExt} does not match any target.RECORD_TYPE`); - return false; - } - return data; // Return the parsed data if validation passes } return 3; // It doesn't exist