Skip to content

Commit

Permalink
ci: check missing env variables within jest tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo Voisin committed Apr 17, 2024
1 parent 4e36579 commit d33f120
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 51 deletions.

This file was deleted.

35 changes: 0 additions & 35 deletions scripts/check_missing_variables_in_env_files_diff.py

This file was deleted.

56 changes: 56 additions & 0 deletions src/noMissingVariablesInEnvFiles.native.test.ts
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
})
})

0 comments on commit d33f120

Please sign in to comment.