cleanup(environment): remove unused layer inventory
This commit is contained in:
@@ -151,7 +151,7 @@ describe('loadLayeredEnv', () => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
it('reports each layer with its absolute path', () => {
|
it('reports each file value with its absolute path', () => {
|
||||||
const home = tmp()
|
const home = tmp()
|
||||||
const project = tmp()
|
const project = tmp()
|
||||||
writeFileSync(join(home, '.env'), `${NAMES[1]}=u\n`)
|
writeFileSync(join(home, '.env'), `${NAMES[1]}=u\n`)
|
||||||
@@ -160,12 +160,8 @@ describe('loadLayeredEnv', () => {
|
|||||||
vi.stubEnv('DSH_HOME', home)
|
vi.stubEnv('DSH_HOME', home)
|
||||||
try {
|
try {
|
||||||
const snapshot = loadLayeredEnv(NAME, project, vi.fn())
|
const snapshot = loadLayeredEnv(NAME, project, vi.fn())
|
||||||
expect(snapshot.layers).toEqual([
|
|
||||||
{ source: 'process' },
|
|
||||||
{ source: 'project-env', path: join(project, '.env') },
|
|
||||||
{ source: 'user-env', path: join(home, '.env') },
|
|
||||||
])
|
|
||||||
expect(snapshot.get(NAMES[1])).toEqual({ value: 'u', source: 'user-env', path: join(home, '.env') })
|
expect(snapshot.get(NAMES[1])).toEqual({ value: 'u', source: 'user-env', path: join(home, '.env') })
|
||||||
|
expect(snapshot.get(NAMES[2])).toEqual({ value: 'p', source: 'project-env', path: join(project, '.env') })
|
||||||
// getFrom is a refusal, not a demotion: an omitted layer is invisible.
|
// getFrom is a refusal, not a demotion: an omitted layer is invisible.
|
||||||
expect(snapshot.getFrom(NAMES[2], ['process', 'user-env'])).toBeUndefined()
|
expect(snapshot.getFrom(NAMES[2], ['process', 'user-env'])).toBeUndefined()
|
||||||
} finally {
|
} finally {
|
||||||
@@ -205,10 +201,8 @@ describe('loadLayeredEnv', () => {
|
|||||||
try {
|
try {
|
||||||
const snapshot = loadLayeredEnv(NAME, project, warn)
|
const snapshot = loadLayeredEnv(NAME, project, warn)
|
||||||
expect(warn).toHaveBeenCalledWith(expect.stringContaining(`${NAME}: failed to load .env`))
|
expect(warn).toHaveBeenCalledWith(expect.stringContaining(`${NAME}: failed to load .env`))
|
||||||
expect(snapshot.layers).toEqual([
|
expect(snapshot.get(NAMES[1])).toBeUndefined()
|
||||||
{ source: 'process' },
|
expect(snapshot.get(NAMES[2])).toEqual({ value: 'project-only', source: 'project-env', path: join(project, '.env') })
|
||||||
{ source: 'project-env', path: join(project, '.env') },
|
|
||||||
])
|
|
||||||
expect(process.env[NAMES[2]]).toBe('project-only')
|
expect(process.env[NAMES[2]]).toBe('project-only')
|
||||||
} finally {
|
} finally {
|
||||||
clear()
|
clear()
|
||||||
@@ -227,10 +221,7 @@ describe('loadLayeredEnv', () => {
|
|||||||
try {
|
try {
|
||||||
const snapshot = loadLayeredEnv(NAME, project)
|
const snapshot = loadLayeredEnv(NAME, project)
|
||||||
expect(write).toHaveBeenCalledWith(expect.stringContaining(`${NAME}: failed to load .env`))
|
expect(write).toHaveBeenCalledWith(expect.stringContaining(`${NAME}: failed to load .env`))
|
||||||
expect(snapshot.layers).toEqual([
|
expect(snapshot.get(NAMES[2])).toEqual({ value: 'project-only', source: 'project-env', path: join(project, '.env') })
|
||||||
{ source: 'process' },
|
|
||||||
{ source: 'project-env', path: join(project, '.env') },
|
|
||||||
])
|
|
||||||
expect(process.env[NAMES[2]]).toBe('project-only')
|
expect(process.env[NAMES[2]]).toBe('project-only')
|
||||||
} finally {
|
} finally {
|
||||||
write.mockRestore()
|
write.mockRestore()
|
||||||
@@ -251,10 +242,7 @@ describe('loadLayeredEnv', () => {
|
|||||||
// layer is simply absent, and nothing is reported.
|
// layer is simply absent, and nothing is reported.
|
||||||
const snapshot = loadLayeredEnv(NAME, project, warn)
|
const snapshot = loadLayeredEnv(NAME, project, warn)
|
||||||
expect(warn).not.toHaveBeenCalled()
|
expect(warn).not.toHaveBeenCalled()
|
||||||
expect(snapshot.layers).toEqual([
|
expect(snapshot.get(NAMES[2])).toEqual({ value: 'project-only', source: 'project-env', path: join(project, '.env') })
|
||||||
{ source: 'process' },
|
|
||||||
{ source: 'project-env', path: join(project, '.env') },
|
|
||||||
])
|
|
||||||
} finally {
|
} finally {
|
||||||
clear()
|
clear()
|
||||||
vi.unstubAllEnvs()
|
vi.unstubAllEnvs()
|
||||||
@@ -269,7 +257,6 @@ describe('loadLayeredEnv', () => {
|
|||||||
vi.stubEnv('APP_BOOT_LAYERED_INHERITED', 'inherited')
|
vi.stubEnv('APP_BOOT_LAYERED_INHERITED', 'inherited')
|
||||||
try {
|
try {
|
||||||
const snapshot = loadLayeredEnv(NAME, project, vi.fn())
|
const snapshot = loadLayeredEnv(NAME, project, vi.fn())
|
||||||
expect(snapshot.layers).toEqual([{ source: 'process' }])
|
|
||||||
expect(snapshot.get('APP_BOOT_LAYERED_INHERITED')).toEqual({ value: 'inherited', source: 'process' })
|
expect(snapshot.get('APP_BOOT_LAYERED_INHERITED')).toEqual({ value: 'inherited', source: 'process' })
|
||||||
} finally {
|
} finally {
|
||||||
clear()
|
clear()
|
||||||
@@ -287,10 +274,6 @@ describe('loadLayeredEnv', () => {
|
|||||||
// is the more trusted of the two — reading it twice would otherwise
|
// is the more trusted of the two — reading it twice would otherwise
|
||||||
// put the same path at two different ranks.
|
// put the same path at two different ranks.
|
||||||
const snapshot = loadLayeredEnv(NAME, both, vi.fn())
|
const snapshot = loadLayeredEnv(NAME, both, vi.fn())
|
||||||
expect(snapshot.layers).toEqual([
|
|
||||||
{ source: 'process' },
|
|
||||||
{ source: 'project-env', path: join(both, '.env') },
|
|
||||||
])
|
|
||||||
expect(snapshot.get(NAMES[2])).toEqual({ value: 'one-file', source: 'project-env', path: join(both, '.env') })
|
expect(snapshot.get(NAMES[2])).toEqual({ value: 'one-file', source: 'project-env', path: join(both, '.env') })
|
||||||
} finally {
|
} finally {
|
||||||
clear()
|
clear()
|
||||||
|
|||||||
@@ -35,13 +35,6 @@ export interface EnvironmentEntry {
|
|||||||
path?: string
|
path?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
/** One environment layer's identity, for diagnostics. */
|
|
||||||
export interface EnvironmentLayer {
|
|
||||||
source: EnvironmentSource
|
|
||||||
/** Absolute path of the file behind this layer; absent for `process`. */
|
|
||||||
path?: string
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The frozen environment of one launch. Construct through
|
* The frozen environment of one launch. Construct through
|
||||||
* {@link createEnvironmentSnapshot}; nothing mutates it afterwards, so a
|
* {@link createEnvironmentSnapshot}; nothing mutates it afterwards, so a
|
||||||
@@ -65,8 +58,6 @@ export interface EnvironmentSnapshot {
|
|||||||
* @returns the first matching entry, or `undefined`.
|
* @returns the first matching entry, or `undefined`.
|
||||||
*/
|
*/
|
||||||
getFrom(name: string, sources: readonly EnvironmentSource[]): EnvironmentEntry | undefined
|
getFrom(name: string, sources: readonly EnvironmentSource[]): EnvironmentEntry | undefined
|
||||||
/** The layers this snapshot was built from, most trusted first. */
|
|
||||||
readonly layers: readonly EnvironmentLayer[]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -121,12 +112,6 @@ export function createEnvironmentSnapshot(layers: readonly EnvironmentLayerInput
|
|||||||
return {
|
return {
|
||||||
get: name => getFrom(name, ENVIRONMENT_SOURCES),
|
get: name => getFrom(name, ENVIRONMENT_SOURCES),
|
||||||
getFrom,
|
getFrom,
|
||||||
layers: ENVIRONMENT_SOURCES
|
|
||||||
.filter(source => bySource.has(source))
|
|
||||||
.map((source): EnvironmentLayer => {
|
|
||||||
const path = bySource.get(source)?.path
|
|
||||||
return { source, ...path === undefined ? {} : { path } }
|
|
||||||
}),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -28,15 +28,6 @@ describe('createEnvironmentSnapshot', () => {
|
|||||||
expect(layered.getFrom('SHARED', [])).toBeUndefined()
|
expect(layered.getFrom('SHARED', [])).toBeUndefined()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('lists its layers in trust order with their paths', () => {
|
|
||||||
expect(layered.layers).toEqual([
|
|
||||||
{ source: 'process' },
|
|
||||||
{ source: 'project-env', path: '/work/.env' },
|
|
||||||
{ source: 'user-env', path: '/home/.dsh/.env' },
|
|
||||||
])
|
|
||||||
expect(createEnvironmentSnapshot([{ source: 'process', values: {} }]).layers).toEqual([{ source: 'process' }])
|
|
||||||
})
|
|
||||||
|
|
||||||
it('copies each layer, so a later mutation of the source object cannot change it', () => {
|
it('copies each layer, so a later mutation of the source object cannot change it', () => {
|
||||||
const values: Record<string, string> = { KEY: 'first' }
|
const values: Record<string, string> = { KEY: 'first' }
|
||||||
const snapshot = createEnvironmentSnapshot([{ source: 'process', values }])
|
const snapshot = createEnvironmentSnapshot([{ source: 'process', values }])
|
||||||
@@ -76,7 +67,6 @@ describe('environmentOf', () => {
|
|||||||
// A host that discovered no files has exactly one layer, so the trusted
|
// A host that discovered no files has exactly one layer, so the trusted
|
||||||
// lookups every consumer makes still find what it was launched with.
|
// lookups every consumer makes still find what it was launched with.
|
||||||
expect(snapshot.getFrom('DSH_ENV_SPEC_FALLBACK', ['process', 'user-env'])?.value).toBe('ambient')
|
expect(snapshot.getFrom('DSH_ENV_SPEC_FALLBACK', ['process', 'user-env'])?.value).toBe('ambient')
|
||||||
expect(snapshot.layers).toEqual([{ source: 'process' }])
|
|
||||||
} finally {
|
} finally {
|
||||||
vi.unstubAllEnvs()
|
vi.unstubAllEnvs()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user