Skip to content

Commit 051a79a

Browse files
committed
Ensure all environment variables are transformed
Ensure that the keys of `ENV_TO_KEY_MAPPING` (the known environment variables that need to be transformed) all have one, and only one, declared transformation mechanism (as `BOOL_KEYS`, `STRING_KEYS`, ...) Use type hints to ensure the members of these lists are valid keys of `ENV_TO_KEY_MAPPING` -- this is not quite the same assertion as the tests, but provides immediate feedback when making a typo.
1 parent e7cf661 commit 051a79a

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

src/__tests__/config.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ import fs from "fs"
33

44
import { VERSION } from "../version"
55
import { Configuration } from "../config"
6+
import {
7+
ENV_TO_KEY_MAPPING,
8+
BOOL_KEYS,
9+
FLOAT_KEYS,
10+
LIST_KEYS,
11+
LIST_OR_BOOL_KEYS,
12+
STRING_KEYS
13+
} from "../config/configmap"
614

715
describe("Configuration", () => {
816
const name = "TEST APP"
@@ -73,6 +81,22 @@ describe("Configuration", () => {
7381
resetEnv()
7482
})
7583

84+
it("knows how to transform all environment variables into options", () => {
85+
const allKeys = Object.keys(ENV_TO_KEY_MAPPING).sort()
86+
87+
const allKeyTransformations = [
88+
BOOL_KEYS,
89+
STRING_KEYS,
90+
LIST_KEYS,
91+
LIST_OR_BOOL_KEYS,
92+
FLOAT_KEYS
93+
]
94+
.flat()
95+
.sort()
96+
97+
expect(allKeys).toEqual(allKeyTransformations)
98+
})
99+
76100
describe("with only default options", () => {
77101
it("loads all default options", () => {
78102
config = new Configuration({})

src/config/configmap.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { AppsignalOptions } from "./options"
22

3-
export const ENV_TO_KEY_MAPPING: Record<string, keyof AppsignalOptions> = {
3+
export const ENV_TO_KEY_MAPPING = {
44
APPSIGNAL_ACTIVE: "active",
55
APPSIGNAL_APP_ENV: "environment",
66
APPSIGNAL_APP_NAME: "name",
@@ -39,7 +39,7 @@ export const ENV_TO_KEY_MAPPING: Record<string, keyof AppsignalOptions> = {
3939
APPSIGNAL_STATSD_PORT: "statsdPort",
4040
APPSIGNAL_WORKING_DIRECTORY_PATH: "workingDirectoryPath",
4141
APP_REVISION: "revision"
42-
}
42+
} satisfies Record<string, keyof AppsignalOptions>
4343

4444
export const PRIVATE_ENV_MAPPING: Record<string, keyof AppsignalOptions> = {
4545
_APPSIGNAL_ACTIVE: "active",
@@ -118,7 +118,7 @@ export const JS_TO_RUBY_MAPPING: Record<keyof AppsignalOptions, string> = {
118118
workingDirectoryPath: "working_directory_path"
119119
}
120120

121-
export const BOOL_KEYS: string[] = [
121+
export const BOOL_KEYS: (keyof typeof ENV_TO_KEY_MAPPING)[] = [
122122
"APPSIGNAL_ACTIVE",
123123
"APPSIGNAL_ENABLE_HOST_METRICS",
124124
"APPSIGNAL_ENABLE_OPENTELEMETRY_HTTP",
@@ -132,7 +132,7 @@ export const BOOL_KEYS: string[] = [
132132
"APPSIGNAL_SEND_SESSION_DATA"
133133
]
134134

135-
export const STRING_KEYS: string[] = [
135+
export const STRING_KEYS: (keyof typeof ENV_TO_KEY_MAPPING)[] = [
136136
"APPSIGNAL_APP_ENV",
137137
"APPSIGNAL_APP_NAME",
138138
"APPSIGNAL_BIND_ADDRESS",
@@ -153,7 +153,7 @@ export const STRING_KEYS: string[] = [
153153
"APP_REVISION"
154154
]
155155

156-
export const LIST_KEYS: string[] = [
156+
export const LIST_KEYS: (keyof typeof ENV_TO_KEY_MAPPING)[] = [
157157
"APPSIGNAL_DNS_SERVERS",
158158
"APPSIGNAL_FILTER_PARAMETERS",
159159
"APPSIGNAL_FILTER_SESSION_DATA",
@@ -163,8 +163,10 @@ export const LIST_KEYS: string[] = [
163163
"APPSIGNAL_REQUEST_HEADERS"
164164
]
165165

166-
export const LIST_OR_BOOL_KEYS: string[] = [
166+
export const LIST_OR_BOOL_KEYS: (keyof typeof ENV_TO_KEY_MAPPING)[] = [
167167
"APPSIGNAL_DISABLE_DEFAULT_INSTRUMENTATIONS"
168168
]
169169

170-
export const FLOAT_KEYS: string[] = ["APPSIGNAL_CPU_COUNT"]
170+
export const FLOAT_KEYS: (keyof typeof ENV_TO_KEY_MAPPING)[] = [
171+
"APPSIGNAL_CPU_COUNT"
172+
]

0 commit comments

Comments
 (0)