-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { describe, expect, it } from 'bun:test'; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
import { name, version } from '../../../package.json'; | ||
import { AtlasValidationError } from '../errors'; | ||
import { getStatsPath, getStatsMetdata, createStatsFile, validateStatsFile } from '../stats'; | ||
|
||
describe('getStatsPath', () => { | ||
it('returns default path `<project>/.expo/stats.jsonl`', () => { | ||
expect(getStatsPath('<project>')).toBe('<project>/.expo/stats.jsonl'); | ||
}); | ||
}); | ||
|
||
describe('getStatsMetadata', () => { | ||
it('returns package name and version', () => { | ||
expect(getStatsMetdata()).toMatchObject({ name, version }); | ||
}); | ||
}); | ||
|
||
describe('createStatsFile', () => { | ||
it('creates a stats file with the correct metadata', async () => { | ||
const file = fixture('create-metadata', { temporary: true }); | ||
await createStatsFile(file); | ||
await expect(fs.promises.readFile(file, 'utf8')).resolves.toBe( | ||
JSON.stringify({ name, version }) + '\n' | ||
); | ||
}); | ||
|
||
it('overwrites invalid stats file', async () => { | ||
const file = fixture('create-invalid', { temporary: true }); | ||
await fs.promises.writeFile(file, JSON.stringify({ name, version: '0.0.0' }) + '\n'); | ||
await createStatsFile(file); | ||
await expect(fs.promises.readFile(file, 'utf8')).resolves.toBe( | ||
JSON.stringify({ name, version }) + '\n' | ||
); | ||
}); | ||
|
||
it('reuses valid stats file', async () => { | ||
const file = fixture('create-valid', { temporary: true }); | ||
await fs.promises.writeFile(file, JSON.stringify({ name, version }) + '\n'); | ||
await createStatsFile(file); | ||
await expect(fs.promises.readFile(file, 'utf-8')).resolves.toBe( | ||
JSON.stringify({ name, version }) + '\n' | ||
); | ||
}); | ||
}); | ||
|
||
describe('validateStatsFile', () => { | ||
it('passes for valid stats file', async () => { | ||
const file = fixture('validate-valid', { temporary: true }); | ||
await createStatsFile(file); | ||
await expect(validateStatsFile(file)).resolves.pass(); | ||
}); | ||
|
||
it('fails for non-existing stats file', async () => { | ||
await expect(validateStatsFile('./this-file-does-not-exists')).rejects.toThrow( | ||
AtlasValidationError | ||
); | ||
}); | ||
|
||
it('fails for invalid stats file', async () => { | ||
const file = fixture('validate-invalid', { temporary: true }); | ||
await fs.promises.writeFile(file, JSON.stringify({ name, version: '0.0.0' }) + '\n'); | ||
await expect(validateStatsFile(file)).rejects.toThrow(AtlasValidationError); | ||
}); | ||
|
||
it('skips validation when EXPO_ATLAS_NO_STATS_VALIDATION is true-ish', async () => { | ||
using _env = env('EXPO_ATLAS_NO_STATS_VALIDATION', 'true'); | ||
const file = fixture('validate-skip-invalid', { temporary: true }); | ||
await fs.promises.writeFile(file, JSON.stringify({ name, version: '0.0.0' }) + '\n'); | ||
await expect(validateStatsFile(file)).resolves.pass(); | ||
}); | ||
}); | ||
|
||
/** | ||
* Get the file path to a fixture, by name. | ||
* This automatically adds the required `.jsonl` or `.temp.jsonl` extension. | ||
* Use `temporary: true` to keep it out of the repository, and reset the content automatically. | ||
*/ | ||
function fixture(name: string, { temporary = false }: { temporary?: boolean } = {}) { | ||
const file = temporary | ||
? path.join(__dirname, 'fixtures/stats', `${name}.temp.jsonl`) | ||
: path.join(__dirname, 'fixtures/stats', `${name}.jsonl`); | ||
|
||
if (temporary) { | ||
fs.writeFileSync(file, ''); | ||
Check failure on line 87 in src/utils/__tests__/stats.test.ts GitHub Actions / coreENOENT: No such file or directory
Check failure on line 87 in src/utils/__tests__/stats.test.ts GitHub Actions / coreENOENT: No such file or directory
Check failure on line 87 in src/utils/__tests__/stats.test.ts GitHub Actions / coreENOENT: No such file or directory
Check failure on line 87 in src/utils/__tests__/stats.test.ts GitHub Actions / coreENOENT: No such file or directory
Check failure on line 87 in src/utils/__tests__/stats.test.ts GitHub Actions / coreENOENT: No such file or directory
Check failure on line 87 in src/utils/__tests__/stats.test.ts GitHub Actions / coreENOENT: No such file or directory
|
||
} | ||
|
||
return file; | ||
} | ||
|
||
/** | ||
* Change the environment variable for the duration of a test. | ||
* This uses explicit resource management to revert the environment variable after the test. | ||
*/ | ||
function env(key: string, value?: string): { key: string; value?: string } & Disposable { | ||
const original = process.env[key]; | ||
|
||
if (value === undefined) { | ||
delete process.env[key]; | ||
} else { | ||
process.env[key] = value; | ||
} | ||
|
||
return { | ||
key, | ||
value, | ||
[Symbol.dispose]() { | ||
if (original === undefined) { | ||
delete process.env[key]; | ||
} else { | ||
process.env[key] = value; | ||
} | ||
}, | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import { boolish } from 'getenv'; | ||
|
||
export const env = { | ||
get EXPO_DEBUG() { | ||
return boolish('EXPO_DEBUG', false); | ||
get EXPO_ATLAS_DEBUG() { | ||
return boolish('EXPO_ATLAS_DEBUG', false); | ||
}, | ||
get EXPO_NO_STATS_VALIDATION() { | ||
return boolish('EXPO_NO_STATS_VALIDATION', false); | ||
get EXPO_ATLAS_NO_STATS_VALIDATION() { | ||
return boolish('EXPO_ATLAS_NO_STATS_VALIDATION', false); | ||
}, | ||
}; |