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
5 changes: 3 additions & 2 deletions packages/sim-cli/src/commands/source-permissions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ describe('Search permission CSV upload', () => {
it('stages immutable bounded parts, preserves CSV records, and activates only after both files', async () => {
const requests: { path: string; body: Record<string, unknown> }[] = []
const released: string[] = []
const users = '\ufeffuser_id,email\r\n42,"person@example.com"\r\n'
const grants = `project_path,user_id\n${'team/repo,42\n'.repeat(5001)}`
const users = '\ufeff user_id , email \r\n \t\r\n 42 , "person@example.com" \r\n'
const grants = `project_path,user_id\n${' "team/repo" , 42 \n'.repeat(5001)}`
const result = await runEmbeddedCli(
args,
{
Expand Down Expand Up @@ -212,6 +212,7 @@ describe('Search permission CSV upload', () => {
projectPermissions: { rowCount: 5001, parts: 2 },
})
const parts = requests.slice(1, -1)
expect(parts[0].body.content).toBe('user_id,email\n42,person@example.com\n')
expect(parts.map(({ body }) => body.field)).toEqual([
'userMapping',
'projectPermissions',
Expand Down
32 changes: 32 additions & 0 deletions packages/sim-cli/src/transfer/permission-csv.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { describe, expect, it, vi } from 'vitest'
import { SimApiError } from '#sim-cli/http/client'
import { stagePermissionCsvs } from '#sim-cli/transfer/permission-csv'

describe('Permission CSV staging errors', () => {
it.each(['input', 'temporary directory'])(
'preserves the cause of a missing %s',
async (missing) => {
const directory = await mkdtemp(join(tmpdir(), 'sim-csv-error-'))
try {
if (missing === 'temporary directory') vi.stubEnv('TMPDIR', join(directory, 'missing'))
const failure = await stagePermissionCsvs('gitlab', {
userMapping: join(directory, 'users.csv'),
projectPermissions: join(directory, 'projects.csv'),
}).catch((error: unknown) => error)
expect(failure).toBeInstanceOf(SimApiError)
expect(failure).toMatchObject({
cause: expect.objectContaining({
code: 'ENOENT',
path: expect.any(String),
syscall: missing === 'input' ? 'open' : 'mkdtemp',
}),
})
} finally {
await rm(directory, { recursive: true, force: true })
}
}
)
})
18 changes: 14 additions & 4 deletions packages/sim-cli/src/transfer/permission-csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ export async function stagePermissionCsvs(
signal?: AbortSignal
) {
const directory = await mkdtemp(join(tmpdir(), 'sim-permissions-')).catch((error: unknown) => {
throw new SimApiError(`Cannot stage permission CSVs: ${getErrorMessage(error)}`, 0)
const failure = new SimApiError(`Cannot stage permission CSVs: ${getErrorMessage(error)}`, 0)
failure.cause = error
throw failure
})
const parts: { field: CsvField; path: string }[] = []
const manifest = {} as Record<CsvField, CsvManifest>
Expand Down Expand Up @@ -143,7 +145,12 @@ export async function stagePermissionCsvs(
parts.push({ field, path: partPath })
totalBytes += partBytes
}
const parser = parse({ bom: true, skip_empty_lines: true, max_record_size: 8192 })
const parser = parse({
bom: true,
trim: true,
skip_empty_lines: true,
max_record_size: 8192,
})
const decoded = Readable.from(decode())
decoded.on('error', (error) => parser.destroy(error))
decoded.pipe(parser)
Expand Down Expand Up @@ -203,8 +210,11 @@ export async function stagePermissionCsvs(
} catch (error) {
await dispose()
if (error instanceof SimApiError) throw error
if (error instanceof Error && 'code' in error && typeof error.code === 'string')
throw new SimApiError(`Cannot stage permission CSVs: ${error.message}`, 0)
if (error instanceof Error && 'code' in error && typeof error.code === 'string') {
const failure = new SimApiError(`Cannot stage permission CSVs: ${error.message}`, 0)
failure.cause = error
throw failure
}
throw error
}
}
Loading