Skip to content

Commit

Permalink
Upgrade to dotenv-java v3.0.0 (#69)
Browse files Browse the repository at this point in the history
* Update Java to 11

- Use Java 11 for maven compiler release
- Bump dependency versions

* Migrate tests to junit 5

- Refactor tests
- Remove deprecations

* Update docs

- Making requirements clear

* Switch to github actions

- Remove .travis.yml

* Add maven-compiler-plugin

* Migrate DotEnvDslTest to jupiter

* Update docs

- Revert back to initial versions for now

* Add dsl tests for systemProperties
  • Loading branch information
amalpotra committed Feb 11, 2024
1 parent f375008 commit bd94e57
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 129 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Build with Maven

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Set up JDK 11
uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'temurin'
cache: maven

- name: Build with Maven
run: mvn clean package dokka:javadocJar jacoco:report

- name: Report Coveralls
uses: coverallsapp/github-action@v2
9 changes: 0 additions & 9 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Contributors are not responsible for pushing packages to mavencentral and jcente
mvn clean package dokka:javadocJar
```

### Publish to Github Packages
### Publish to GitHub Packages

Add `distributionManagement` to `pom.xml`

Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ With **Java**
import io.github.cdimascio.dotenv.Dotenv;

Dotenv dotenv = Dotenv.load();
dotenv.get("MY_ENV_VAR1")
dotenv.get("MY_ENV_VAR1");
```

or with **Kotlin**
Expand Down Expand Up @@ -217,7 +217,7 @@ for (e in dotenv.entries()) {
Dotenv
.configure()
.filename("myenv")
.load()
.load();
```
**Kotlin Dsl example**

Expand All @@ -237,7 +237,7 @@ for (e in dotenv.entries()) {
Dotenv
.configure()
.ignoreIfMalformed()
.load()
.load();
```
**Kotlin Dsl example**

Expand All @@ -257,7 +257,7 @@ for (e in dotenv.entries()) {
Dotenv
.configure()
.ignoreIfMissing()
.load()
.load();
```
**Kotlin Dsl example**

Expand All @@ -277,7 +277,7 @@ for (e in dotenv.entries()) {
Dotenv
.configure()
.systemProperties()
.load()
.load();
```
**Kotlin Dsl example**

Expand Down
71 changes: 23 additions & 48 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,36 +48,25 @@
</developer>
</developers>


<properties>
<dotenv.version>2.3.2</dotenv.version>
<kotlin.compiler.jvmTarget>1.8</kotlin.compiler.jvmTarget>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<kotlin.version>1.6.0</kotlin.version>
<dotenv.version>3.0.0</dotenv.version>
<maven.compiler.release>11</maven.compiler.release>
<kotlin.version>1.9.22</kotlin.version>
<main.class>io.cdimascio.DotenvKt</main.class>
<junit.version>4.13.1</junit.version>
<junit.version>5.10.1</junit.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.source.plugin>3.0.1</maven.source.plugin>
<maven.javadoc.plugin>3.0.0</maven.javadoc.plugin>
<maven.jar.plugin>2.6</maven.jar.plugin>
<maven.jacoco.plugin>0.8.4</maven.jacoco.plugin>
<maven.coveralls.plugin>4.3.0</maven.coveralls.plugin>
<maven.surefire.plugin>2.22.0</maven.surefire.plugin>
<dokka.version>1.4.0</dokka.version>
<maven.compiler.plugin>3.12.1</maven.compiler.plugin>
<maven.source.plugin>3.3.0</maven.source.plugin>
<maven.javadoc.plugin>3.6.3</maven.javadoc.plugin>
<maven.jar.plugin>3.3.0</maven.jar.plugin>
<maven.jacoco.plugin>0.8.11</maven.jacoco.plugin>
<maven.surefire.plugin>3.2.3</maven.surefire.plugin>
<dokka.version>1.9.10</dokka.version>
<bintray.subject>cdimascio</bintray.subject>
<bintray.repo>maven</bintray.repo>
<bintray.package>dotenv-kotlin</bintray.package>
</properties>

<pluginRepositories>
<pluginRepository>
<id>jcenter</id>
<name>JCenter</name>
<url>https://jcenter.bintray.com/</url>
</pluginRepository>
</pluginRepositories>

<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
Expand All @@ -94,8 +83,8 @@
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
Expand All @@ -111,6 +100,13 @@
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<plugins>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin}</version>
</plugin>

<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
Expand All @@ -119,6 +115,7 @@
<executions>
<execution>
<id>compile</id>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
Expand Down Expand Up @@ -156,13 +153,6 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.plugin}</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit4</artifactId>
<version>2.22.0</version>
</dependency>
</dependencies>
<configuration>
<includes>
<include>**/*.java</include>
Expand Down Expand Up @@ -246,25 +236,10 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.eluder.coveralls</groupId>
<artifactId>coveralls-maven-plugin</artifactId>
<version>${maven.coveralls.plugin}</version>
<configuration>
<repoToken>DTE7449Zg7tuwgrV6HeE0et1YB6JPPfeY</repoToken>
</configuration>
<dependencies>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.3</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.7</version>
<version>1.6.13</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
Expand Down Expand Up @@ -305,7 +280,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<version>3.1.0</version>
<executions>
<execution>
<id>sign-artifacts</id>
Expand Down
66 changes: 26 additions & 40 deletions src/test/java/tests/JavaTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,32 @@
import io.github.cdimascio.dotenv.Dotenv;
import io.github.cdimascio.dotenv.DotenvEntry;
import io.github.cdimascio.dotenv.DotenvException;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;

public class JavaTests {
private Map<String, String> envVars;

@Before
public void setUp() {
envVars = new HashMap<>();
envVars.put("MY_TEST_EV1", "my test ev 1");
envVars.put("MY_TEST_EV2", "my test ev 2");
envVars.put("WITHOUT_VALUE", "");
}
private final Map<String, String> envVars = Map.of(
"MY_TEST_EV1", "my test ev 1",
"MY_TEST_EV2", "my test ev 2",
"WITHOUT_VALUE", ""
);

@Test(expected = DotenvException.class)
@Test
public void throwIfMalconfigured() {
Dotenv.configure().load();
assertThrows(DotenvException.class, () -> Dotenv.configure().load());
}

@Test(expected = DotenvException.class)
@Test
public void load() {
Dotenv dotenv = Dotenv.load();

for (String envName : envVars.keySet()) {
assertEquals(envVars.get(envName), dotenv.get(envName));
}
assertThrows(DotenvException.class, () -> {
Dotenv dotenv = Dotenv.load();
envVars.keySet().forEach(envName -> assertEquals(envVars.get(envName), dotenv.get(envName)));
});
}

@Test
Expand All @@ -44,13 +37,7 @@ public void iteratorOverDotenv() {
.ignoreIfMalformed()
.load();

dotenv
.entries()
.forEach(e -> assertEquals(dotenv.get(e.getKey()), e.getValue()));

for (DotenvEntry e : dotenv.entries()) {
assertEquals(dotenv.get(e.getKey()), e.getValue());
}
dotenv.entries().forEach(e -> assertEquals(dotenv.get(e.getKey()), e.getValue()));
}

@Test
Expand All @@ -63,32 +50,31 @@ public void iteratorOverDotenvWithFilter() {
Set<DotenvEntry> entriesAll = dotenv.entries();
assertTrue(entriesInFile.size() < entriesAll.size());

for (Map.Entry<String, String> e: envVars.entrySet()) {
assertEquals(dotenv.get(e.getKey()), e.getValue());
}
envVars.forEach((key, value) -> assertEquals(dotenv.get(key), value));
}

@Test(expected = UnsupportedOperationException.class)
@Test
public void failToRemoveFromDotenv() {
Dotenv dotenv = Dotenv.configure()
.ignoreIfMalformed()
.load();

Iterator<DotenvEntry> iter = dotenv.entries().iterator();
while (iter.hasNext()) {
iter.next();
iter.remove();
}
assertThrows(UnsupportedOperationException.class, () -> {
Iterator<DotenvEntry> iter = dotenv.entries().iterator();
while (iter.hasNext()) {
iter.next();
iter.remove();
}
});
}

@Test(expected = UnsupportedOperationException.class)
@Test
public void failToAddToDotenv() {

Dotenv dotenv = Dotenv.configure()
.ignoreIfMalformed()
.load();

dotenv.entries().add(new DotenvEntry("new", "value"));
assertThrows(UnsupportedOperationException.class, () -> dotenv.entries().add(new DotenvEntry("new", "value")));
}

@Test
Expand Down
27 changes: 16 additions & 11 deletions src/test/kotlin/tests/BasicTests.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package tests

import io.github.cdimascio.dotenv.DotenvException
import io.github.cdimascio.dotenv.Dotenv
import io.github.cdimascio.dotenv.DotenvException
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import org.junit.Test as test
import org.junit.jupiter.api.Test as test

class DotEnvTest {
private val envVars = mapOf(
Expand All @@ -15,11 +16,13 @@ class DotEnvTest {
"MULTI_LINE" to "hello\\nworld"
)

@test(expected = DotenvException::class)
@test
fun dotenvMalformed() {
Dotenv.configure()
.directory("./src/test/resources")
.load()
assertFailsWith<DotenvException> {
Dotenv.configure()
.directory("./src/test/resources")
.load()
}
}

@test
Expand Down Expand Up @@ -113,11 +116,13 @@ class DotEnvTest {
}
}

@test(expected = DotenvException::class)
@test
fun dotenvMissing() {
Dotenv.configure()
.directory("/missing/.env")
.load()
assertFailsWith<DotenvException> {
Dotenv.configure()
.directory("/missing/.env")
.load()
}
}

@test
Expand All @@ -133,7 +138,7 @@ class DotEnvTest {
}

private fun assertHostEnvVar(env: Dotenv) {
val isWindows = System.getProperty("os.name").toLowerCase().indexOf("win") >= 0
val isWindows = System.getProperty("os.name").lowercase().indexOf("win") >= 0
if (isWindows) {
val path = env["PATH"]
assertNotNull(path)
Expand Down
Loading

0 comments on commit bd94e57

Please sign in to comment.