Files
flutter_dotenv/test/empty_env_test.dart
2025-08-20 12:56:58 +12:00

53 lines
1.5 KiB
Dart

import 'dart:io';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
group('loadFromString empty .env file handling', () {
setUp(() {
dotenv.clean();
});
test('empty .env file with isOptional=true should not throw', () {
expect(() {
dotenv.loadFromString(
envString: File('test/.env.empty').readAsStringSync(),
isOptional: true);
}, returnsNormally);
expect(dotenv.isInitialized, isTrue);
expect(dotenv.env.isEmpty, isTrue);
});
test('empty .env file with isOptional=false should throw FileEmptyError',
() {
// This test verifies that an empty .env file still throws when isOptional is false
expect(() {
dotenv.loadFromString(
envString: File('test/.env.empty').readAsStringSync(),
isOptional: false);
}, throwsA(isA<EmptyEnvFileError>()));
});
test('missing .env file with isOptional=true should not throw', () {
File emptyFile = File('test/.env.empty');
expect(() {
dotenv.loadFromString(
envString: emptyFile.readAsStringSync(), isOptional: true);
}, returnsNormally);
expect(dotenv.isInitialized, isTrue);
expect(dotenv.env.isEmpty, isTrue);
});
test(
'missing .env file with isOptional=false should throw FileNotFoundError',
() {
expect(() {
dotenv.loadFromString(envString: '', isOptional: false);
}, throwsA(isA<EmptyEnvFileError>()));
});
});
}