Add .env file support for option --dart-define-from-file (#128668)

# Proposal

I suggest to make possible to specify .env files to the --dart-define-from-file in addition to the Json format.

# Issue

Close #128667
This commit is contained in:
Piotr FLEURY
2023-07-05 18:35:08 +02:00
committed by GitHub
parent 35085c394d
commit 168d807734
2 changed files with 254 additions and 10 deletions

View File

@@ -518,7 +518,8 @@ void main() {
"kDouble": 1.1,
"name": "denghaizhu",
"title": "this is title from config json file",
"nullValue": null
"nullValue": null,
"containEqual": "sfadsfv=432f"
}
'''
);
@@ -549,6 +550,7 @@ void main() {
'name=denghaizhu',
'title=this is title from config json file',
'nullValue=null',
'containEqual=sfadsfv=432f',
'body=this is body from config json file',
]),
);
@@ -557,6 +559,155 @@ void main() {
ProcessManager: () => FakeProcessManager.any(),
});
testUsingContext('--dart-define-from-file correctly parses a valid env file', () async {
globals.fs
.file(globals.fs.path.join('lib', 'main.dart'))
.createSync(recursive: true);
globals.fs.file('pubspec.yaml').createSync();
globals.fs.file('.packages').createSync();
await globals.fs.file('.env').writeAsString('''
# comment
kInt=1
kDouble=1.1 # should be double
name=piotrfleury
title=this is title from config env file
empty=
doubleQuotes="double quotes 'value'#=" # double quotes
singleQuotes='single quotes "value"#=' # single quotes
backQuotes=`back quotes "value" '#=` # back quotes
hashString="some-#-hash-string-value"
# Play around with spaces around the equals sign.
spaceBeforeEqual =value
spaceAroundEqual = value
spaceAfterEqual= value
''');
await globals.fs.file('.env2').writeAsString('''
# second comment
body=this is body from config env file
''');
final CommandRunner<void> runner =
createTestCommandRunner(BuildBundleCommand(
logger: BufferLogger.test(),
));
await runner.run(<String>[
'bundle',
'--no-pub',
'--dart-define-from-file=.env',
'--dart-define-from-file=.env2',
]);
}, overrides: <Type, Generator>{
BuildSystem: () => TestBuildSystem.all(BuildResult(success: true),
(Target target, Environment environment) {
expect(
_decodeDartDefines(environment),
containsAllInOrder(const <String>[
'kInt=1',
'kDouble=1.1',
'name=piotrfleury',
'title=this is title from config env file',
'empty=',
"doubleQuotes=double quotes 'value'#=",
'singleQuotes=single quotes "value"#=',
'backQuotes=back quotes "value" \'#=',
'hashString=some-#-hash-string-value',
'spaceBeforeEqual=value',
'spaceAroundEqual=value',
'spaceAfterEqual=value',
'body=this is body from config env file'
]),
);
}),
FileSystem: fsFactory,
ProcessManager: () => FakeProcessManager.any(),
});
testUsingContext('--dart-define-from-file option env file throws a ToolExit when .env file contains a multiline value', () async {
globals.fs
.file(globals.fs.path.join('lib', 'main.dart'))
.createSync(recursive: true);
globals.fs.file('pubspec.yaml').createSync();
globals.fs.file('.packages').createSync();
await globals.fs.file('.env').writeAsString('''
# single line value
name=piotrfleury
# multi-line value
multiline = """ Welcome to .env demo
a simple counter app with .env file support
for more info, check out the README.md file
Thanks! """ # This is the welcome message that will be displayed on the counter app
''');
final CommandRunner<void> runner =
createTestCommandRunner(BuildBundleCommand(
logger: BufferLogger.test(),
));
expect(() => runner.run(<String>[
'bundle',
'--no-pub',
'--dart-define-from-file=.env',
]), throwsToolExit(message: 'Multi-line value is not supported: multiline = """ Welcome to .env demo'));
}, overrides: <Type, Generator>{
BuildSystem: () => TestBuildSystem.all(BuildResult(success: true)),
FileSystem: fsFactory,
ProcessManager: () => FakeProcessManager.any(),
});
testUsingContext('--dart-define-from-file option works with mixed file formats',
() async {
globals.fs
.file(globals.fs.path.join('lib', 'main.dart'))
.createSync(recursive: true);
globals.fs.file('pubspec.yaml').createSync();
globals.fs.file('.packages').createSync();
await globals.fs.file('.env').writeAsString('''
kInt=1
kDouble=1.1
name=piotrfleury
title=this is title from config env file
''');
await globals.fs.file('config.json').writeAsString('''
{
"body": "this is body from config json file"
}
''');
final CommandRunner<void> runner =
createTestCommandRunner(BuildBundleCommand(
logger: BufferLogger.test(),
));
await runner.run(<String>[
'bundle',
'--no-pub',
'--dart-define-from-file=.env',
'--dart-define-from-file=config.json',
]);
}, overrides: <Type, Generator>{
BuildSystem: () => TestBuildSystem.all(BuildResult(success: true),
(Target target, Environment environment) {
expect(
_decodeDartDefines(environment),
containsAllInOrder(const <String>[
'kInt=1',
'kDouble=1.1',
'name=piotrfleury',
'title=this is title from config env file',
'body=this is body from config json file',
]),
);
}),
FileSystem: fsFactory,
ProcessManager: () => FakeProcessManager.any(),
});
testUsingContext('test --dart-define-from-file option if conflict', () async {
globals.fs.file(globals.fs.path.join('lib', 'main.dart')).createSync(recursive: true);
globals.fs.file('pubspec.yaml').createSync();