Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix and actually test test_data/native_dynamic_linking #1524

Merged
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
1 change: 1 addition & 0 deletions pkgs/native_assets_builder/dart_test.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
paths:
- test/build_runner/
- test/model/
- test/test_data/
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ void main(List<String> arguments) {
final addLibrary = DynamicLibrary.open(addLibraryPath);
final add = addLibrary.lookupFunction<Int32 Function(Int32, Int32),
int Function(int, int)>('add');
print(add(a, b));
print('Result: ${add(a, b)}');
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,110 +17,111 @@ import 'package:test/test.dart';
import '../helpers.dart';

void main() async {
late Uri tempUri;
const name = 'native_dynamic_linking';

setUp(() async {
tempUri = (await Directory.systemTemp.createTemp()).uri;
});

tearDown(() async {
await Directory.fromUri(tempUri).delete(recursive: true);
});

for (final dryRun in [true, false]) {
final testSuffix = dryRun ? ' dry_run' : '';
test('native_dynamic_linking build$testSuffix', () async {
final testTempUri = tempUri.resolve('test1/');
await Directory.fromUri(testTempUri).create();
final testPackageUri = testDataUri.resolve('$name/');
final dartUri = Uri.file(Platform.resolvedExecutable);
test(
'native_dynamic_linking build$testSuffix',
() => inTempDir((tempUri) async {
final testTempUri = tempUri.resolve('test1/');
await Directory.fromUri(testTempUri).create();
final testPackageUri = testDataUri.resolve('$name/');
final dartUri = Uri.file(Platform.resolvedExecutable);

final config = BuildConfigImpl(
outputDirectory: tempUri,
packageName: name,
packageRoot: testPackageUri,
targetOS: OSImpl.current,
version: HookConfigImpl.latestVersion,
linkModePreference: LinkModePreferenceImpl.dynamic,
dryRun: dryRun,
linkingEnabled: false,
targetArchitecture: dryRun ? null : ArchitectureImpl.current,
buildMode: dryRun ? null : BuildModeImpl.debug,
cCompiler: dryRun
? null
: CCompilerConfigImpl(
compiler: cc,
envScript: envScript,
envScriptArgs: envScriptArgs,
),
);

final buildConfigUri = testTempUri.resolve('build_config.json');
File.fromUri(buildConfigUri)
.writeAsStringSync(jsonEncode(config.toJson()));
final config = BuildConfigImpl(
outputDirectory: tempUri,
packageName: name,
packageRoot: testPackageUri,
targetOS: OSImpl.current,
version: HookConfigImpl.latestVersion,
linkModePreference: LinkModePreferenceImpl.dynamic,
dryRun: dryRun,
linkingEnabled: false,
targetArchitecture: dryRun ? null : ArchitectureImpl.current,
buildMode: dryRun ? null : BuildModeImpl.debug,
cCompiler: dryRun
? null
: CCompilerConfigImpl(
compiler: cc,
envScript: envScript,
envScriptArgs: envScriptArgs,
),
);

final processResult = await Process.run(
dartUri.toFilePath(),
[
'hook/build.dart',
'--config=${buildConfigUri.toFilePath()}',
],
workingDirectory: testPackageUri.toFilePath(),
);
if (processResult.exitCode != 0) {
print(processResult.stdout);
print(processResult.stderr);
print(processResult.exitCode);
}
expect(processResult.exitCode, 0);
final buildConfigUri = testTempUri.resolve('build_config.json');
File.fromUri(buildConfigUri)
.writeAsStringSync(jsonEncode(config.toJson()));

final buildOutputUri = tempUri.resolve('build_output.json');
final buildOutput = HookOutputImpl.fromJsonString(
await File.fromUri(buildOutputUri).readAsString());
final assets = buildOutput.assets;
final dependencies = buildOutput.dependencies;
if (dryRun) {
expect(assets.length, greaterThanOrEqualTo(3));
expect(dependencies, <Uri>[]);
} else {
expect(assets.length, 3);
expect(await assets.allExist(), true);
expect(
dependencies,
final processResult = await Process.run(
dartUri.toFilePath(),
[
testPackageUri.resolve('src/debug.c'),
testPackageUri.resolve('src/math.c'),
testPackageUri.resolve('src/add.c'),
'hook/build.dart',
'--config=${buildConfigUri.toFilePath()}',
],
workingDirectory: testPackageUri.toFilePath(),
);
if (processResult.exitCode != 0) {
print(processResult.stdout);
print(processResult.stderr);
print(processResult.exitCode);
}
expect(processResult.exitCode, 0);

final addLibraryPath = assets
.firstWhere((asset) => asset.id.endsWith('add.dart'))
.file!
.toFilePath();
final addResult = await runProcess(
executable: dartExecutable,
arguments: [
'run',
pkgNativeAssetsBuilderUri
.resolve('test/test_data/native_dynamic_linking_add.dart')
.toFilePath(),
addLibraryPath,
'1',
'2',
],
environment: {
// Add the directory containing the linked dynamic libraries to the
// PATH so that the dynamic linker can find them.
if (Platform.isWindows)
'PATH': '${tempUri.toFilePath()};${Platform.environment['PATH']}',
},
throwOnUnexpectedExitCode: true,
logger: logger,
);
expect(addResult.stdout, 'Adding 1 and 2.\n3\n');
}
});
final buildOutputUri = tempUri.resolve('build_output.json');
final buildOutput = HookOutputImpl.fromJsonString(
await File.fromUri(buildOutputUri).readAsString());
final assets = buildOutput.assets;
final dependencies = buildOutput.dependencies;
if (dryRun) {
expect(assets.length, greaterThanOrEqualTo(3));
expect(dependencies, <Uri>[]);
} else {
expect(assets.length, 3);
expect(await assets.allExist(), true);
expect(
dependencies,
[
testPackageUri.resolve('src/debug.c'),
testPackageUri.resolve('src/math.c'),
testPackageUri.resolve('src/add.c'),
],
);

final addLibraryPath = assets
.firstWhere((asset) => asset.id.endsWith('add.dart'))
.file!
.toFilePath();
final addResult = await runProcess(
executable: dartExecutable,
arguments: [
'run',
pkgNativeAssetsBuilderUri
.resolve('test/test_data/native_dynamic_linking_helper.dart')
.toFilePath(),
addLibraryPath,
'1',
'2',
],
environment: {
// Add the directory containing the linked dynamic libraries to
// the PATH so that the dynamic linker can find them.
if (Platform.isWindows)
'PATH':
'${tempUri.toFilePath()};${Platform.environment['PATH']}',
},
throwOnUnexpectedExitCode: true,
logger: logger,
);
expect(
addResult.stdout,
allOf(
contains('Adding 1 and 2.'),
contains('Result: 3'),
),
);
}
}),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ extension on BuildConfig {
'-l$libraryName',
],
OS.windows => [
outputDirectory.resolve('$libraryName.dll').toFilePath(),
outputDirectory.resolve('$libraryName.lib').toFilePath(),
],
_ => throw UnimplementedError('Unsupported OS: $targetOS'),
};
Expand Down