Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/src/env.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ final class Env {
as T;
}

if (currentValue == null && defaultValue != null) {
return defaultValue;
if (currentValue == null || currentValue is MissingValue && defaultValue != null) {
return defaultValue as T;
}

return currentValue as T;
Expand Down
17 changes: 16 additions & 1 deletion test/env_guard_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ final class Env implements DefineEnvironment {
static final String host = 'HOST';
static final String port = 'PORT';
static final String uri = 'URI';
static final String optionalKey = 'OPTIONAL_KEY';

@override
final Map<String, EnvSchema> schema = {
host: env.string().optional(),
port: env.number().integer(),
uri: env.string(),
optionalKey: env.string().optional(),
};
}

Expand Down Expand Up @@ -48,7 +50,10 @@ void main() {
HOST=localhost
''');

env.define(root: directory, {'PORT': env.number().integer(), 'HOST': env.string()});
env.define(root: directory, {
'PORT': env.number().integer(),
'HOST': env.string(),
});

expect(env.get('PORT'), 8080);
expect(env.get('HOST'), 'localhost');
Expand Down Expand Up @@ -93,4 +98,14 @@ void main() {
test('should get null value when environment has not property', () {
expect(env.get('PORT'), isNull);
});

test('should apply defaultValue when environment has not property', () {
const attemptValue = "Hello World";
expect(env.get('DONT_EXISTS', defaultValue: attemptValue), attemptValue);
});

test('should apply defaultValue when environment has optional property', () {
const attemptValue = "Hello World";
expect(env.get(Env.optionalKey, defaultValue: attemptValue), attemptValue);
});
}