-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci: check missing env variables within jest tests
- Loading branch information
Hugo Voisin
committed
Apr 17, 2024
1 parent
4e36579
commit d33f120
Showing
3 changed files
with
56 additions
and
51 deletions.
There are no files selected for viewing
16 changes: 0 additions & 16 deletions
16
.github/workflows/dev_on_pull_request_missing_variables_in_env_files.yml
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import fs from 'fs' | ||
|
||
function loadEnvVariables(filePath: string) { | ||
if (!fs.existsSync(filePath)) { | ||
throw new Error(`File not found: ${filePath}`) | ||
} | ||
const content = fs.readFileSync(filePath, 'utf-8') | ||
const lines = content.split('\n') | ||
|
||
const envVars: string[] = [] | ||
lines.forEach((line) => { | ||
line = line.trim() | ||
if (line && !line.startsWith('#')) { | ||
const [key] = line.split('=') | ||
if (key) envVars.push(key.trim()) | ||
} | ||
}) | ||
|
||
return envVars | ||
} | ||
|
||
function compareEnvFiles(envFiles: string[]) { | ||
const envData: Record<string, string[]> = {} | ||
envFiles.forEach((file) => { | ||
envData[file] = loadEnvVariables(file) | ||
}) | ||
|
||
const allKeys = new Set<string>() | ||
for (const data of Object.values(envData)) { | ||
for (const key of data) { | ||
allKeys.add(key) | ||
} | ||
} | ||
|
||
const missing: Record<string, string[]> = {} | ||
for (const [file, data] of Object.entries(envData)) { | ||
missing[file] = Array.from(allKeys).filter((x) => !data.includes(x)) | ||
} | ||
|
||
return missing | ||
} | ||
|
||
describe('.env files', () => { | ||
const envFiles = ['.env.testing', '.env.staging', '.env.integration', '.env.production'] | ||
|
||
test('all variables should be present in all .env files', () => { | ||
const missingVariables = compareEnvFiles(envFiles) | ||
for (const [file, variables] of Object.entries(missingVariables)) { | ||
if (variables.length > 0) { | ||
throw new Error(`Missing variables in ${file}: ${variables.join(', ')}`) | ||
} | ||
} | ||
|
||
expect(true).toBe(true) // Pass if no error is thrown | ||
}) | ||
}) |