Skip to content

Commit

Permalink
Linting & CI fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisreddington committed Nov 5, 2024
1 parent a40f0e6 commit 4fb2eb5
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 125 deletions.
210 changes: 111 additions & 99 deletions __tests__/main.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const core = require('@actions/core');
const main = require('../src/main');
const fs = require('fs');
const core = require('@actions/core')
const main = require('../src/main')
const fs = require('fs')

// Mock fs module with constants and promises
jest.mock('fs', () => ({
Expand Down Expand Up @@ -28,16 +28,16 @@ jest.mock('fs', () => ({
W_OK: 2,
X_OK: 1
}
}));
}))

const getInputMock = jest.spyOn(core, 'getInput').mockImplementation();
const setFailedMock = jest.spyOn(core, 'setFailed').mockImplementation();
const infoMock = jest.spyOn(core, 'info').mockImplementation();
const getInputMock = jest.spyOn(core, 'getInput').mockImplementation()
const setFailedMock = jest.spyOn(core, 'setFailed').mockImplementation()
const infoMock = jest.spyOn(core, 'info').mockImplementation()

describe('devcontainer-validator', () => {
beforeEach(() => {
jest.clearAllMocks();
});
jest.clearAllMocks()
})

describe('validateExtensions', () => {
it('should return empty array when all extensions are present', () => {
Expand All @@ -47,12 +47,15 @@ describe('devcontainer-validator', () => {
extensions: ['ext1', 'ext2', 'ext3']
}
}
};
const requiredExtensions = ['ext1', 'ext2'];

const result = main.validateExtensions(devcontainerContent, requiredExtensions);
expect(result).toEqual([]);
});
}
const requiredExtensions = ['ext1', 'ext2']

const result = main.validateExtensions(
devcontainerContent,
requiredExtensions
)
expect(result).toEqual([])
})

it('should return missing extensions when some are not configured', () => {
const devcontainerContent = {
Expand All @@ -61,12 +64,14 @@ describe('devcontainer-validator', () => {
extensions: ['ext1']
}
}
};
const requiredExtensions = ['ext1', 'ext2', 'ext3'];

const result = main.validateExtensions(devcontainerContent, requiredExtensions);
expect(result).toEqual(['ext2', 'ext3']);
});
}
const requiredExtensions = ['ext1', 'ext2', 'ext3']
const result = main.validateExtensions(
devcontainerContent,
requiredExtensions
)
expect(result).toEqual(['ext2', 'ext3'])
})

it('should handle empty extensions configuration', () => {
const devcontainerContent = {
Expand All @@ -75,13 +80,16 @@ describe('devcontainer-validator', () => {
extensions: []
}
}
};
const requiredExtensions = ['ext1', 'ext2'];

const result = main.validateExtensions(devcontainerContent, requiredExtensions);
expect(result).toEqual(['ext1', 'ext2']);
});
});
}
const requiredExtensions = ['ext1', 'ext2']

const result = main.validateExtensions(
devcontainerContent,
requiredExtensions
)
expect(result).toEqual(['ext1', 'ext2'])
})
})

describe('validateTasks', () => {
it('should return null when all required tasks are present', () => {
Expand All @@ -91,30 +99,30 @@ describe('devcontainer-validator', () => {
test: 'go test ./...',
run: 'go run .'
}
};
const result = main.validateTasks(devcontainerContent);
expect(result).toBeNull();
});
}

const result = main.validateTasks(devcontainerContent)
expect(result).toBeNull()
})

it('should return error when tasks property is missing', () => {
const devcontainerContent = {};
const result = main.validateTasks(devcontainerContent);
expect(result).toBe("'tasks' property is missing");
});
const devcontainerContent = {}

const result = main.validateTasks(devcontainerContent)
expect(result).toBe("'tasks' property is missing")
})

it('should return error when required tasks are missing', () => {
const devcontainerContent = {
tasks: {
build: 'go build .',
test: 'go test ./...'
}
};
const result = main.validateTasks(devcontainerContent);
expect(result).toBe('Missing or invalid required tasks: run');
});
}

const result = main.validateTasks(devcontainerContent)
expect(result).toBe('Missing or invalid required tasks: run')
})

it('should return error when tasks are not strings', () => {
const devcontainerContent = {
Expand All @@ -123,12 +131,12 @@ describe('devcontainer-validator', () => {
test: 'go test ./...',
run: 'go run .'
}
};
const result = main.validateTasks(devcontainerContent);
expect(result).toBe('Missing or invalid required tasks: build');
});
});
}

const result = main.validateTasks(devcontainerContent)
expect(result).toBe('Missing or invalid required tasks: build')
})
})

describe('run', () => {
it('should pass when all extensions are present', async () => {
Expand All @@ -138,45 +146,47 @@ describe('devcontainer-validator', () => {
extensions: ['ext1', 'ext2']
}
}
};
}

getInputMock.mockImplementation(name => {
switch (name) {
case 'extensions-list':
return 'ext1,ext2';
return 'ext1,ext2'
case 'validate-tasks':
return 'false';
return 'false'
default:
return '.devcontainer/devcontainer.json';
return '.devcontainer/devcontainer.json'
}
});
})

fs.existsSync.mockReturnValue(true);
fs.readFileSync.mockReturnValue(JSON.stringify(mockDevcontainer));
fs.existsSync.mockReturnValue(true)
fs.readFileSync.mockReturnValue(JSON.stringify(mockDevcontainer))

await main.run();
expect(infoMock).toHaveBeenCalledWith('All validations passed successfully');
});
await main.run()
expect(infoMock).toHaveBeenCalledWith(
'All validations passed successfully'
)
})

it('should fail when devcontainer.json is not found', async () => {
getInputMock.mockImplementation(name => {
switch (name) {
case 'extensions-list':
return 'ext1,ext2';
return 'ext1,ext2'
case 'validate-tasks':
return 'false';
return 'false'
default:
return '.devcontainer/devcontainer.json';
return '.devcontainer/devcontainer.json'
}
});
})

fs.existsSync.mockReturnValue(false);
fs.existsSync.mockReturnValue(false)

await main.run();
await main.run()
expect(setFailedMock).toHaveBeenCalledWith(
expect.stringContaining('devcontainer.json not found')
);
});
)
})

it('should fail when required extensions are missing', async () => {
const mockDevcontainer = {
Expand All @@ -185,27 +195,27 @@ describe('devcontainer-validator', () => {
extensions: ['ext1']
}
}
};
}

getInputMock.mockImplementation(name => {
switch (name) {
case 'extensions-list':
return 'ext1,ext2';
return 'ext1,ext2'
case 'validate-tasks':
return 'false';
return 'false'
default:
return '.devcontainer/devcontainer.json';
return '.devcontainer/devcontainer.json'
}
});
})

fs.existsSync.mockReturnValue(true);
fs.readFileSync.mockReturnValue(JSON.stringify(mockDevcontainer));
fs.existsSync.mockReturnValue(true)
fs.readFileSync.mockReturnValue(JSON.stringify(mockDevcontainer))

await main.run();
await main.run()
expect(setFailedMock).toHaveBeenCalledWith(
expect.stringContaining('Missing required extensions: ext2')
);
});
)
})

it('should validate tasks when validate-tasks is true', async () => {
const mockDevcontainer = {
Expand All @@ -219,25 +229,27 @@ describe('devcontainer-validator', () => {
test: 'test cmd',
run: 'run cmd'
}
};
}

getInputMock.mockImplementation(name => {
switch (name) {
case 'extensions-list':
return 'ext1';
return 'ext1'
case 'validate-tasks':
return 'true';
return 'true'
default:
return '.devcontainer/devcontainer.json';
return '.devcontainer/devcontainer.json'
}
});
})

fs.existsSync.mockReturnValue(true);
fs.readFileSync.mockReturnValue(JSON.stringify(mockDevcontainer));
fs.existsSync.mockReturnValue(true)
fs.readFileSync.mockReturnValue(JSON.stringify(mockDevcontainer))

await main.run();
expect(infoMock).toHaveBeenCalledWith('All validations passed successfully');
});
await main.run()
expect(infoMock).toHaveBeenCalledWith(
'All validations passed successfully'
)
})

it('should fail when tasks validation fails', async () => {
const mockDevcontainer = {
Expand All @@ -249,26 +261,26 @@ describe('devcontainer-validator', () => {
tasks: {
build: 'build cmd'
}
};
}

getInputMock.mockImplementation(name => {
switch (name) {
case 'extensions-list':
return 'ext1';
return 'ext1'
case 'validate-tasks':
return 'true';
return 'true'
default:
return '.devcontainer/devcontainer.json';
return '.devcontainer/devcontainer.json'
}
});
})

fs.existsSync.mockReturnValue(true);
fs.readFileSync.mockReturnValue(JSON.stringify(mockDevcontainer));
fs.existsSync.mockReturnValue(true)
fs.readFileSync.mockReturnValue(JSON.stringify(mockDevcontainer))

await main.run();
await main.run()
expect(setFailedMock).toHaveBeenCalledWith(
expect.stringContaining('Missing or invalid required tasks: test, run')
);
});
});
});
)
})
})
})
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ inputs:
default: 'false'

runs:
using: node22
using: node20
main: dist/index.js
Loading

0 comments on commit 4fb2eb5

Please sign in to comment.