[tool] Proposal to support dart define config from a json file (#108098)

This commit is contained in:
haizhu
2022-09-27 05:47:27 +08:00
committed by GitHub
parent 50cd73775f
commit cbfe5a5226
6 changed files with 254 additions and 3 deletions

View File

@@ -290,4 +290,33 @@ void main() {
],
});
});
testUsingContext('test --dart-define-from-file option with err json format', () async {
await globals.fs.file('config.json').writeAsString(
'''
{
"kInt": 1,
"kDouble": 1.1,
"name": "err json format,
"title": "this is title from config json file"
}
'''
);
final CommandRunner<void> commandRunner = createTestCommandRunner(AssembleCommand(
buildSystem: TestBuildSystem.all(BuildResult(success: true)),
));
expect(commandRunner.run(<String>['assemble',
'-o Output',
'debug_macos_bundle_flutter_assets',
'--dart-define=k=v',
'--dart-define-from-file=config.json']),
throwsToolExit(message: 'Json config define file "--dart-define-from-file=config.json" format err, please fix first! format err:'));
}, overrides: <Type, Generator>{
Cache: () => Cache.test(processManager: FakeProcessManager.any()),
FileSystem: () => MemoryFileSystem.test(),
ProcessManager: () => FakeProcessManager.any(),
});
}

View File

@@ -4,6 +4,7 @@
import 'package:args/command_runner.dart';
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/common.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/build_info.dart';
import 'package:flutter_tools/src/build_system/build_system.dart';
@@ -463,6 +464,64 @@ void main() {
FileSystem: fsFactory,
ProcessManager: () => FakeProcessManager.any(),
});
testUsingContext('test --dart-define-from-file option', () 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('config.json').writeAsString(
'''
{
"kInt": 1,
"kDouble": 1.1,
"name": "denghaizhu",
"title": "this is title from config json file"
}
'''
);
final CommandRunner<void> runner = createTestCommandRunner(BuildBundleCommand());
await runner.run(<String>[
'bundle',
'--no-pub',
'--dart-define-from-file=config.json',
]);
}, overrides: <Type, Generator>{
BuildSystem: () => TestBuildSystem.all(BuildResult(success: true), (Target target, Environment environment) {
expect(environment.defines[kDartDefines], 'a0ludD0x,a0RvdWJsZT0xLjE=,bmFtZT1kZW5naGFpemh1,dGl0bGU9dGhpcyBpcyB0aXRsZSBmcm9tIGNvbmZpZyBqc29uIGZpbGU=');
}),
FileSystem: fsFactory,
ProcessManager: () => FakeProcessManager.any(),
});
testUsingContext('test --dart-define-from-file option by corrupted json', () 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('config.json').writeAsString(
'''
{
"kInt": 1Error json format
"kDouble": 1.1,
"name": "denghaizhu",
"title": "this is title from config json file"
}
'''
);
final CommandRunner<void> runner = createTestCommandRunner(BuildBundleCommand());
expect(() => runner.run(<String>[
'bundle',
'--no-pub',
'--dart-define-from-file=config.json',
]), throwsA(predicate<Exception>((Exception e) => e is ToolExit && e.message!.startsWith('Json config define file "--dart-define-from-file=config.json" format err'))));
}, overrides: <Type, Generator>{
FileSystem: fsFactory,
BuildSystem: () => TestBuildSystem.all(BuildResult(success: true)),
ProcessManager: () => FakeProcessManager.any(),
});
}
class FakeBundleBuilder extends Fake implements BundleBuilder {

View File

@@ -7,6 +7,7 @@ import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/build_info.dart';
import '../src/common.dart';
import '../src/context.dart';
void main() {
late BufferLogger logger;
@@ -279,4 +280,70 @@ void main() {
kDartDefines: 'MTIzMiw0NTY=,Mg==',
}, kDartDefines), <String>['1232,456', '2']);
});
group('Check repeated buildInfo variables', () {
testUsingContext('toEnvironmentConfig repeated variable', () async {
const BuildInfo buildInfo = BuildInfo(BuildMode.debug, '',
treeShakeIcons: true,
trackWidgetCreation: true,
dartDefines: <String>['foo=2', 'bar=2'],
dartDefineConfigJsonMap: <String,Object>{ 'DART_DEFINES' : 'Define a variable, but it occupies the variable name of the system'},
dartObfuscation: true,
);
buildInfo.toEnvironmentConfig();
expect(testLogger.warningText, contains('The key: [DART_DEFINES] already exists, you cannot use environment variables that have been used by the system'));
});
testUsingContext('toEnvironmentConfig repeated variable with DART_DEFINES not set', () async {
// Simulate operation flutterCommand.getBuildInfo with `dart-define-from-file` set dartDefines
const BuildInfo buildInfo = BuildInfo(BuildMode.debug, '',
treeShakeIcons: true,
dartDefines: <String>['DART_DEFINES=Define a variable, but it occupies the variable name of the system'],
trackWidgetCreation: true,
dartDefineConfigJsonMap: <String, Object>{ 'DART_DEFINES' : 'Define a variable, but it occupies the variable name of the system'},
dartObfuscation: true,
);
buildInfo.toEnvironmentConfig();
expect(testLogger.warningText, contains('The key: [DART_DEFINES] already exists, you cannot use environment variables that have been used by the system'));
});
testUsingContext('toGradleConfig repeated variable', () async {
const BuildInfo buildInfo = BuildInfo(BuildMode.debug, '',
treeShakeIcons: true,
trackWidgetCreation: true,
dartDefines: <String>['foo=2', 'bar=2'],
dartDefineConfigJsonMap: <String,Object>{ 'dart-defines' : 'Define a variable, but it occupies the variable name of the system'},
dartObfuscation: true,
);
buildInfo.toGradleConfig();
expect(testLogger.warningText, contains('he key: [dart-defines] already exists, you cannot use gradle variables that have been used by the system'));
});
testUsingContext('toGradleConfig repeated variable with not set', () async {
// Simulate operation flutterCommand.getBuildInfo with `dart-define-from-file` set dartDefines
const BuildInfo buildInfo = BuildInfo(BuildMode.debug, '',
treeShakeIcons: true,
trackWidgetCreation: true,
dartDefines: <String>['dart-defines=Define a variable, but it occupies the variable name of the system'],
dartDefineConfigJsonMap: <String,Object>{ 'dart-defines' : 'Define a variable, but it occupies the variable name of the system'},
dartObfuscation: true,
);
buildInfo.toGradleConfig();
expect(testLogger.warningText, contains('he key: [dart-defines] already exists, you cannot use gradle variables that have been used by the system'));
});
testUsingContext('toGradleConfig with androidProjectArgs override gradle project variant', () async {
const BuildInfo buildInfo = BuildInfo(BuildMode.debug, '',
treeShakeIcons: true,
trackWidgetCreation: true,
androidProjectArgs: <String>['applicationId=com.google'],
dartDefineConfigJsonMap: <String,Object>{ 'applicationId' : 'override applicationId'},
dartObfuscation: true,
);
buildInfo.toGradleConfig();
expect(testLogger.warningText, contains('The key: [applicationId] already exists, you cannot use gradle variables that have been used'));
});
});
}