mirror of
https://github.com/actions/github-script.git
synced 2026-06-07 01:24:24 +00:00
refactor: extract working-directory validation into testable module
- 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
This commit is contained in:
parent
5ac99427a6
commit
7b9b30a1ea
4 changed files with 99 additions and 4 deletions
43
__test__/working-directory.test.ts
Normal file
43
__test__/working-directory.test.ts
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
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)
|
||||
})
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue