Skip to content

Commit 6e67c69

Browse files
committed
support env variables
1 parent e92e2d4 commit 6e67c69

File tree

8 files changed

+162
-6
lines changed

8 files changed

+162
-6
lines changed

docs/pages/_meta.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ export default {
33
"index": "Getting started",
44
"writing-tests" : "Writing tests",
55
"stubbing" : "Stubbing responses",
6+
"environment-variables" : "Environment variables",
67
"faq" : "Tips and FAQ's"
78
};
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
title: Environment variables
3+
---
4+
import { Callout } from 'nextra/components'
5+
6+
# Configuring environment variables
7+
8+
Orbital supports using [environment variables inside annotations](https://orbitalhq.com/docs/deploying/configuring-orbital#environment-variables-in-annotations),
9+
which can lead to compilation errors if not provided.
10+
11+
## Global config
12+
To use default, global config, simply drop an `env.conf` file in the `test-resources` directory:
13+
14+
```
15+
/
16+
├── src
17+
│ └── schema.taxi
18+
├── test
19+
│ └── SchemaSpec.kt
20+
└── test-resources
21+
└── env.conf <--- env.conf goes here
22+
taxi.conf
23+
```
24+
25+
Orbital uses [HOCON](https://github.com/lightbend/config#examples-of-hocon) format for config files. You can read more about configuring Orbital [here](https://orbitalhq.com/docs/deploying/configuring-orbital)
26+
27+
Here's a sample config file:
28+
29+
```env.conf
30+
TransactionsTopicName = Transactions
31+
```
32+
33+
Hocon is very flexible, you can use a variety of styles:
34+
* `foo.bar=10`
35+
* `foo : 10`
36+
* `foo=10, bar=10`
37+
38+
Read more in the [HOCON docs](https://github.com/lightbend/config?tab=readme-ov-file#examples-of-hocon)
39+
40+
## Overriding environment variables per-spec
41+
42+
Environment variables can be overridden within a spec by calling one of these functions:
43+
44+
```kotlin
45+
fun environmentVariables(vararg pairs: Pair<String, String>)
46+
fun environmentVariables(env: Map<String, String>)
47+
fun env(key: String, value: String)
48+
```
49+
50+
<Callout>Environment variables are set per-spec, and must be set *before* the `describe()` block. Attempting to modify environment variables in a `describe` or `it( .. )` block will result in an error</Callout>
51+
52+
```kotlin
53+
class OverridingGlobalEnvVariablesSpec : OrbitalSpec({
54+
// Override a single env variable
55+
env("click-events-topic-name", "clickstream")
56+
57+
// alternatively, override a bunch:
58+
environmentVariables(
59+
"MyTopic" to "Foo",
60+
"AnotherTopic" to "Bar"
61+
)
62+
describe("overriding global env config variables") {
63+
it("should compile using the overridden env variable") {
64+
// test continues
65+
}
66+
}
67+
})
68+
```

docs/pages/index.mdx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@ You also need a `settings.gradle.kts` (but can be blank)
3535
// This file can be left blank
3636
```
3737

38+
Here's the directory structure:
39+
40+
```
41+
├── src
42+
│ └── schema.taxi
43+
├── test
44+
│ └── SchemaSpec.kt
45+
└── test-resources (Optional)
46+
└── env.conf (Optional)
47+
taxi.conf
48+
build.gradle.kts
49+
settings.gradle.kts (Often blank)
50+
```
51+
3852
Then, to write your first test, create a folder `test` next to your `src` folder.
3953
By convention, these are named ending in `Spec.kt` or `Test.kt`.
4054

docs/theme.config.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export default {
3232
docsRepositoryBase: 'https://github.com/orbitalapi/preflight/docs',
3333
footer: {
3434
content: (
35-
<span>Built by &nbsp;<a className="underline" href="https://orbitalhq.com" target="_blank">Orbital</a>, to make demos suck less.</span>
35+
<span>Built by &nbsp;<a className="underline" href="https://orbitalhq.com" target="_blank">Orbital</a>.</span>
3636
)
3737
},
3838
head: () => {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.orbitalhq.preflight.dsl
2+
3+
import com.typesafe.config.ConfigFactory
4+
5+
/**
6+
* Loads global env variables from test-resources/env.conf
7+
*/
8+
object EnvVariablesLoader {
9+
10+
fun loadGlobalEnvVariables(): Map<String, String> {
11+
return ConfigFactory.load("env.conf")
12+
.root()
13+
.unwrapped()
14+
.mapValues { (_,value) -> value.toString() }
15+
}
16+
17+
private fun tryLoadFromClasspath(): java.io.InputStream? {
18+
return PreflightExtension::class.java.getResourceAsStream("env.conf")
19+
}
20+
21+
22+
}

preflight-core/preflight-runtime/src/main/kotlin/com/orbitalhq/preflight/dsl/OrbitalSpec.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ abstract class OrbitalSpec(body: OrbitalSpec.() -> Unit) : DescribeSpec() {
2525
this.body()
2626
}
2727

28+
fun environmentVariables(vararg pairs: Pair<String, String>) = preflight.environmentVariables(*pairs)
29+
fun environmentVariables(env: Map<String, String>) = preflight.environmentVariables(env)
30+
fun env(key: String, value: String) = preflight.env(key,value)
31+
2832
/**
2933
* Provides access to the compiled taxi document.
3034
* This is lower-level than Orbital's schema object

preflight-core/preflight-runtime/src/main/kotlin/com/orbitalhq/preflight/dsl/PreflightExtension.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,7 @@ class PreflightExtension(val projectRoot: Path = Paths.get("./")) : BeforeSpecLi
6868

6969

7070
override suspend fun beforeSpec(spec: Spec) {
71-
val loader = TaxiPackageLoader.forDirectoryContainingTaxiFile(projectRoot.absolute().normalize())
72-
this.taxiProject = loader.load()
73-
74-
sourcePackage = loadSourcePackage(this.taxiProject.packageRootPath!!)
75-
71+
envVariableContainer.markImmutable()
7672
withClue("Taxi project should compile without errors") {
7773
val taxiSchema = try {
7874
TaxiSchema.from(
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.orbitalhq.preflight.dsl
2+
3+
interface EnvVariableContainer {
4+
fun environmentVariables(vararg pairs: Pair<String, String>)
5+
fun environmentVariables(env: Map<String, String>)
6+
fun env(key: String, value: String)
7+
fun markImmutable()
8+
9+
val envVariables:Map<String,String>
10+
}
11+
12+
class SpecEnvVariables private constructor(globalVariables:Map<String,String>) : EnvVariableContainer {
13+
private val variables = globalVariables.toMutableMap()
14+
private var isMutable = true
15+
companion object {
16+
fun newInstance():SpecEnvVariables {
17+
return SpecEnvVariables(EnvVariablesLoader.loadGlobalEnvVariables())
18+
}
19+
}
20+
21+
override val envVariables: Map<String, String>
22+
get() {
23+
return variables.toMap()
24+
}
25+
26+
override fun markImmutable() {
27+
this.isMutable = false
28+
}
29+
30+
private fun requireIsMutable() {
31+
require(isMutable) { "Modifying env variables after the test has started is not allowed. Configure the env variables before the describe { ... } block"}
32+
}
33+
/**
34+
* Configure environment variables for this specific test spec.
35+
* These will override any global defaults with the same key.
36+
*/
37+
override fun environmentVariables(vararg pairs: Pair<String, String>) {
38+
requireIsMutable()
39+
variables.putAll(pairs)
40+
}
41+
42+
override fun environmentVariables(env: Map<String, String>) {
43+
requireIsMutable()
44+
variables.putAll(env)
45+
}
46+
47+
override fun env(key: String, value: String) {
48+
requireIsMutable()
49+
variables[key] = value
50+
}
51+
}

0 commit comments

Comments
 (0)