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
7 changes: 6 additions & 1 deletion modules/domain/lib/env/env_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@ class EnvConfig {

static String get envConfigFile => 'env/.env.example';

static String get apiUrl => dotenv.env['API_URL_$env']?.toString() ?? '';
/// Supports both env file layouts:
/// - one file per flavor (e.g. `env/.dev`) with an unsuffixed `API_URL`;
/// - a single file with suffixed keys (`API_URL_DEV`, `API_URL_QA`,
/// `API_URL_PROD`) as in `env/.env.example`. A suffixed key wins.
static String get apiUrl =>
dotenv.env['API_URL_$env'] ?? dotenv.env['API_URL'] ?? '';
}
42 changes: 42 additions & 0 deletions modules/domain/test/env/env_config_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:domain/env/env_config.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
tearDown(() => EnvConfig.env = EnvConfig.kDevEnv);

group('EnvConfig.apiUrl', () {
test('reads the unsuffixed API_URL of a per-flavor file', () {
dotenv.testLoad(fileInput: 'API_URL=https://dev.example.com');
EnvConfig.env = EnvConfig.kDevEnv;

expect(EnvConfig.apiUrl, 'https://dev.example.com');
});

test('reads the key suffixed with the active flavor', () {
dotenv.testLoad(
fileInput: 'API_URL_DEV=https://dev.example.com\n'
'API_URL_QA=https://qa.example.com',
);
EnvConfig.env = EnvConfig.kQaEnv;

expect(EnvConfig.apiUrl, 'https://qa.example.com');
});

test('prefers the suffixed key over the unsuffixed one', () {
dotenv.testLoad(
fileInput: 'API_URL=https://fallback.example.com\n'
'API_URL_PROD=https://prod.example.com',
);
EnvConfig.env = EnvConfig.kProdEnv;

expect(EnvConfig.apiUrl, 'https://prod.example.com');
});

test('is empty when no API_URL key is defined', () {
dotenv.testLoad(fileInput: 'ENV=dev');

expect(EnvConfig.apiUrl, isEmpty);
});
});
}