Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81362,12 +81362,26 @@ const artifact = __nccwpck_require__(97676);
const fs = (__nccwpck_require__(79896).promises);
const yaml = __nccwpck_require__(24852);

/**
* Check if a file exists
* @param {string} filePath - Path to the file
* @returns {Promise<boolean>} - True if file exists, false otherwise
*/
async function fileExists(filePath) {
try {
await fs.access(filePath);
return true;
} catch {
return false;
}
}

/**
* Upload artifacts when DEBUG mode is enabled
* @param {string[]} files - Path to the output file
* @param {object} [options] - Optional dependencies for testing
* @param {object} [options.artifactClient] - Artifact client instance (defaults to DefaultArtifactClient)
* @param {function} [options.fileExists] - Function to check if file exists (defaults to fs.access)
* @param {function} [options.fileExists] - Function to check if file exists, returns Promise<boolean> (defaults to fileExists)
* @returns {Promise<{uploaded: boolean, id?: number, size?: number, error?: string, reason?: string}>}
*/
async function uploadArtifacts(files, options = {}) {
Expand All @@ -81381,19 +81395,17 @@ async function uploadArtifacts(files, options = {}) {

// Allow dependency injection for testing
const artifactClient = options.artifactClient || new artifact.DefaultArtifactClient();
const fileExists = options.fileExists || (async (path) => {
await fs.access(path);
return true;
});
const checkFileExists = options.fileExists || fileExists;

const filesToUpload = [];

for (const file of files) {
// Check which files exist and add them to upload list
try {
await fileExists(file);
const exists = await checkFileExists(file);
if (exists) {
core.info(`Found artifact file: ${file}`);
filesToUpload.push(file);
} catch {
} else {
core.warning(`${file} not found, skipping artifact upload for this file`);
}
}
Expand Down Expand Up @@ -81536,7 +81548,7 @@ async function run() {

// Read the output file contents
let outputContent = '';
if (fs.stat(outputFilePath)) {
if (await fileExists(outputFilePath)) {
outputContent = await fs.readFile(outputFilePath, 'utf8');
} else {
core.warning(`Output file ${outputFilePath} not found, using console output instead`);
Expand Down Expand Up @@ -81564,7 +81576,7 @@ async function run() {
run();

// Export for testing
module.exports = { run, uploadArtifacts };
module.exports = { run, uploadArtifacts, fileExists };


/***/ }),
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

32 changes: 22 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,26 @@ const artifact = require('@actions/artifact');
const fs = require('fs').promises;
const yaml = require('js-yaml');

/**
* Check if a file exists
* @param {string} filePath - Path to the file
* @returns {Promise<boolean>} - True if file exists, false otherwise
*/
async function fileExists(filePath) {
try {
await fs.access(filePath);
return true;
} catch {
return false;
}
}

/**
* Upload artifacts when DEBUG mode is enabled
* @param {string[]} files - Path to the output file
* @param {object} [options] - Optional dependencies for testing
* @param {object} [options.artifactClient] - Artifact client instance (defaults to DefaultArtifactClient)
* @param {function} [options.fileExists] - Function to check if file exists (defaults to fs.access)
* @param {function} [options.fileExists] - Function to check if file exists, returns Promise<boolean> (defaults to fileExists)
* @returns {Promise<{uploaded: boolean, id?: number, size?: number, error?: string, reason?: string}>}
*/
async function uploadArtifacts(files, options = {}) {
Expand All @@ -23,19 +37,17 @@ async function uploadArtifacts(files, options = {}) {

// Allow dependency injection for testing
const artifactClient = options.artifactClient || new artifact.DefaultArtifactClient();
const fileExists = options.fileExists || (async (path) => {
await fs.access(path);
return true;
});
const checkFileExists = options.fileExists || fileExists;

const filesToUpload = [];

for (const file of files) {
// Check which files exist and add them to upload list
try {
await fileExists(file);
const exists = await checkFileExists(file);
if (exists) {
core.info(`Found artifact file: ${file}`);
filesToUpload.push(file);
} catch {
} else {
core.warning(`${file} not found, skipping artifact upload for this file`);
}
}
Expand Down Expand Up @@ -178,7 +190,7 @@ async function run() {

// Read the output file contents
let outputContent = '';
if (fs.stat(outputFilePath)) {
if (await fileExists(outputFilePath)) {
outputContent = await fs.readFile(outputFilePath, 'utf8');
} else {
core.warning(`Output file ${outputFilePath} not found, using console output instead`);
Expand Down Expand Up @@ -206,4 +218,4 @@ async function run() {
run();

// Export for testing
module.exports = { run, uploadArtifacts };
module.exports = { run, uploadArtifacts, fileExists };
4 changes: 2 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ describe('artifact upload logic', () => {

it('should upload only streetrace.log when OUTPUT.md does not exist', async () => {
mockFileExists
.mockRejectedValueOnce(new Error('ENOENT')) // OUTPUT.md does not exist
.mockResolvedValueOnce(false) // OUTPUT.md does not exist
.mockResolvedValueOnce(true); // streetrace.log exists
mockArtifactClient.uploadArtifact.mockResolvedValue({ id: 789, size: 256 });

Expand All @@ -469,7 +469,7 @@ describe('artifact upload logic', () => {
});

it('should return no_files reason when no files exist to upload', async () => {
mockFileExists.mockRejectedValue(new Error('ENOENT'));
mockFileExists.mockResolvedValue(false);

const result = await uploadArtifacts(['OUTPUT.md'], {
artifactClient: mockArtifactClient,
Expand Down
Loading