mirror of
https://github.com/actions/github-script.git
synced 2026-06-06 17:14:23 +00:00
- Add src/working-directory.ts with validateWorkingDirectory() - Add __test__/working-directory.test.ts with 4 test cases - Update main.ts to use validation function - Throw clear error for non-existent or non-directory paths - Rebuild dist/index.js Addresses review feedback on #426
43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
import * as fs from 'fs'
|
|
import * as os from 'os'
|
|
import * as path from 'path'
|
|
import {validateWorkingDirectory} from '../src/working-directory'
|
|
|
|
describe('validateWorkingDirectory', () => {
|
|
let tmpDir: string
|
|
|
|
beforeEach(() => {
|
|
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'github-script-test-'))
|
|
})
|
|
|
|
afterEach(() => {
|
|
fs.rmSync(tmpDir, {recursive: true, force: true})
|
|
})
|
|
|
|
test('returns resolved path for existing directory', () => {
|
|
const result = validateWorkingDirectory(tmpDir)
|
|
expect(result).toBe(tmpDir)
|
|
})
|
|
|
|
test('resolves relative paths', () => {
|
|
const result = validateWorkingDirectory(tmpDir)
|
|
expect(path.isAbsolute(result)).toBe(true)
|
|
})
|
|
|
|
test('throws for non-existent directory', () => {
|
|
const nonExistent = path.join(tmpDir, 'does-not-exist')
|
|
expect(() => validateWorkingDirectory(nonExistent)).toThrow(
|
|
/does not exist/
|
|
)
|
|
expect(() => validateWorkingDirectory(nonExistent)).toThrow(nonExistent)
|
|
})
|
|
|
|
test('throws for file path instead of directory', () => {
|
|
const filePath = path.join(tmpDir, 'file.txt')
|
|
fs.writeFileSync(filePath, 'content')
|
|
expect(() => validateWorkingDirectory(filePath)).toThrow(
|
|
/is not a directory/
|
|
)
|
|
expect(() => validateWorkingDirectory(filePath)).toThrow(filePath)
|
|
})
|
|
})
|