Skip to content

Commit eafce27

Browse files
committed
fix: isEveryDefined() now throws NotInitializedError before load()
1 parent 8cae9e7 commit eafce27

4 files changed

Lines changed: 31 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Release notes are available on [github][notes].
1010

1111
# 6.0.1
1212

13+
- [fix] `isEveryDefined()` now throws `NotInitializedError` before `load()`, consistent with all other read APIs
1314
- [fix] Replace `assert()` with explicit `if`/`throw` in `getInt()`, `getDouble()`, and `getBool()` so null-safety checks are enforced in release builds
1415
- Error messages now include the variable name for easier debugging
1516
- [fix] `load(isOptional: true)` no longer discards successfully loaded base file when an override file is missing or empty (fixes #70, #93, #101, #125)

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,9 @@ export EXPORTED=hello
204204
**Parsing rules:**
205205
- Lines without `=` are ignored.
206206
- `#` starts a comment when it appears outside quoted values.
207-
- Double-quoted values expand `\n` to newlines and interpolate `$VAR` / `${VAR}`.
207+
- Double-quoted values unescape `\"` and expand `\n` to newlines, and interpolate `$VAR` / `${VAR}`.
208208
- Single-quoted values do not interpolate variables. Escaped single quotes (`\'`) are unescaped.
209+
- Use `\$` for a literal dollar sign in unquoted or double-quoted values.
209210
- Undefined variables interpolate to an empty string.
210211
- Unquoted values are trimmed of surrounding whitespace.
211212
- If the same key appears multiple times anywhere in the processed input, the first occurrence wins (see [value precedence](#value-precedence)).

lib/src/dotenv.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,10 @@ class DotEnv {
205205
_isInitialized = true;
206206
}
207207

208-
/// True if all supplied variables have nonempty value; false otherwise.
209-
/// Differs from [containsKey](dart:core) by excluding null values.
210-
/// Note [load] should be called first.
208+
/// Returns true if all supplied variables have non-empty values.
209+
/// Throws [NotInitializedError] if called before [load] or [loadFromString].
211210
bool isEveryDefined(Iterable<String> vars) =>
212-
vars.every((k) => _envMap[k]?.isNotEmpty ?? false);
211+
vars.every((k) => env[k]?.isNotEmpty ?? false);
213212

214213
Future<List<String>> _getEntriesFromFile(String filename) async {
215214
try {

test/dotenv_test.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,31 @@ void main() {
177177
});
178178
});
179179

180+
group('isEveryDefined', () {
181+
test('returns true when all variables exist with non-empty values', () {
182+
dotenv.loadFromString(envString: 'A=1\nB=2\nC=3');
183+
expect(dotenv.isEveryDefined(['A', 'B', 'C']), isTrue);
184+
});
185+
186+
test('returns false when a variable is missing', () {
187+
dotenv.loadFromString(envString: 'A=1\nB=2');
188+
expect(dotenv.isEveryDefined(['A', 'B', 'MISSING']), isFalse);
189+
});
190+
191+
test('returns false when a variable has an empty value', () {
192+
dotenv.loadFromString(envString: 'A=1\nB=');
193+
expect(dotenv.isEveryDefined(['A', 'B']), isFalse);
194+
});
195+
196+
test('throws NotInitializedError before initialization', () {
197+
final fresh = DotEnv();
198+
expect(
199+
() => fresh.isEveryDefined(['A']),
200+
throwsA(isA<NotInitializedError>()),
201+
);
202+
});
203+
});
204+
180205
group('isOptional override loading', () {
181206
test(
182207
'loadFromString with valid base and empty override preserves base vars',

0 commit comments

Comments
 (0)