-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(BSR) ci(env): check variables in env files #6318
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { config } from 'dotenv' | ||
|
||
function loadEnvVariables(filePath: string) { | ||
const { parsed, error } = config({ path: filePath }) | ||
if (error) { | ||
throw error | ||
} | ||
if (!parsed) { | ||
throw new Error(`No variables found in ${filePath}`) | ||
} | ||
|
||
return Object.keys(parsed) | ||
} | ||
|
||
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)) | ||
} | ||
Comment on lines
+28
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Les Set ont des méthodes pour les comparer https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set#set_composition j'ai l'impression que A.symmetricDifference(B) répondrait au besoin There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
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 | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
j'ai l'impression qu'il pourrait etre intéressant d'avoir un
Set
en valeurThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Si on avait la méthode Set.union et Set.symetricDifference, carrément, mais ce n'est pas le cas en NodeJS, donc c'est + simple avec un array :/