Skip to content

Commit bc1b32f

Browse files
[ffigen] Move tmpDir into Context and use spaces in temp directory names (dart-lang#3029)
1 parent f208cfc commit bc1b32f

30 files changed

Lines changed: 112 additions & 52 deletions

‎pkgs/ffigen/lib/src/context.dart‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'dart:ffi';
6+
import 'dart:io';
67

78
import 'package:logging/logging.dart';
89

@@ -31,20 +32,30 @@ class Context {
3132
final Scope rootScope = Scope.createRoot('root');
3233
final Scope rootObjCScope = Scope.createRoot('objc_root');
3334
late final ExtraSymbols extraSymbols;
34-
35-
Context(this.logger, FfiGenerator generator, {Uri? libclangDylib})
36-
: config = Config(generator),
37-
cursorIndex = CursorIndex(logger) {
35+
final String tmpDir;
36+
37+
Context(
38+
this.logger,
39+
FfiGenerator generator, {
40+
Uri? libclangDylib,
41+
String? tmpDir,
42+
}) : config = Config(generator),
43+
cursorIndex = CursorIndex(logger),
44+
tmpDir =
45+
tmpDir ??
46+
Directory.systemTemp.createTempSync('ffigen temp dir ').path {
3847
objCBuiltInFunctions = ObjCBuiltInFunctions(
3948
this,
4049
// ignore: deprecated_member_use_from_same_package
4150
generator.objectiveC?.generateForPackageObjectiveC ?? false,
4251
);
52+
4353
final libclangDylibPath =
4454
// ignore: deprecated_member_use_from_same_package
4555
generator.libclangDylib?.toFilePath() ??
4656
libclangDylib?.toFilePath() ??
4757
findDylibAtDefaultLocations(logger);
58+
4859
_clang ??= Clang(DynamicLibrary.open(libclangDylibPath));
4960
}
5061
}

‎pkgs/ffigen/lib/src/header_parser/sub_parsers/macro_parser.dart‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import 'package:path/path.dart' as p;
1212
import '../../code_generator.dart';
1313
import '../../config_provider/config_types.dart';
1414
import '../../context.dart';
15-
import '../../strings.dart' as strings;
1615
import '../clang_bindings/clang_bindings.dart' as clang_types;
1716
import '../utils.dart';
1817

@@ -178,7 +177,7 @@ late Set<String> _macroVarNames;
178177

179178
/// Creates a temporary file for parsing macros in current directory.
180179
File createFileForMacros(Context context) {
181-
final fileNameBase = p.normalize(p.join(strings.tmpDir, 'temp_for_macros'));
180+
final fileNameBase = p.normalize(p.join(context.tmpDir, 'temp_for_macros'));
182181
final fileExt = 'hpp';
183182

184183
// Find a filename which doesn't already exist.

‎pkgs/ffigen/lib/src/strings.dart‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -280,19 +280,6 @@ const synthUsrChar = '~';
280280
const ffiNative = 'ffi-native';
281281
const ffiNativeAsset = 'asset-id';
282282

283-
Directory? _tmpDir;
284-
285-
/// A path to a unique temporary directory that should be used for files meant
286-
/// to be discarded after the current execution is finished.
287-
String get tmpDir {
288-
if (Platform.environment.containsKey('TEST_TMPDIR')) {
289-
return Platform.environment['TEST_TMPDIR']!;
290-
}
291-
292-
_tmpDir ??= Directory.systemTemp.createTempSync();
293-
return _tmpDir!.path;
294-
}
295-
296283
const ffigenJsonSchemaIndent = ' ';
297284
const ffigenJsonSchemaId = 'https://json.schemastore.org/ffigen';
298285
const ffigenJsonSchemaFileName = 'ffigen.schema.json';

‎pkgs/ffigen/test/code_generator_tests/code_generator_test.dart‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -815,10 +815,16 @@ void main() {
815815

816816
/// Utility to match expected bindings to the generated bindings.
817817
void _matchLib(Library lib, String testName) {
818-
matchLibraryWithExpected(lib, 'code_generator_test_${testName}_output.dart', [
819-
'test',
820-
'code_generator_tests',
821-
'expected_bindings',
822-
'_expected_${testName}_bindings.dart',
823-
]);
818+
final context = testContext();
819+
matchLibraryWithExpected(
820+
context,
821+
lib,
822+
'code_generator_test_${testName}_output.dart',
823+
[
824+
'test',
825+
'code_generator_tests',
826+
'expected_bindings',
827+
'_expected_${testName}_bindings.dart',
828+
],
829+
);
824830
}

‎pkgs/ffigen/test/collision_tests/decl_decl_collision_test.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ void main() {
8383
], context),
8484
);
8585
matchLibraryWithExpected(
86+
context,
8687
library,
8788
'decl_decl_collision_test_output.dart',
8889
[

‎pkgs/ffigen/test/collision_tests/decl_symbol_address_collision_test.dart‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ void main() {
4949
);
5050
});
5151
test('declaration and symbol address conflict', () {
52+
final context = testContext();
5253
matchLibraryWithExpected(
54+
context,
5355
actual,
5456
'collision_test_decl_symbol_address_collision_output.dart',
5557
[

‎pkgs/ffigen/test/collision_tests/decl_type_name_collision_test.dart‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ ${strings.headers}:
2828
});
2929

3030
test('Expected bindings', () {
31+
final context = testContext();
3132
matchLibraryWithExpected(
33+
context,
3234
actual,
3335
'decl_type_name_collision_test_output.dart',
3436
[

‎pkgs/ffigen/test/collision_tests/reserved_keyword_collision_test.dart‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import '../test_utils.dart';
1212
void main() {
1313
group('reserved_keyword_collision_test', () {
1414
test('reserved keyword collision', () {
15+
final context = testContext();
1516
final library = parser.parse(
1617
testContext(
1718
FfiGenerator(
@@ -45,6 +46,7 @@ void main() {
4546
),
4647
);
4748
matchLibraryWithExpected(
49+
context,
4850
library,
4951
'reserved_keyword_collision_test_output.dart',
5052
[

‎pkgs/ffigen/test/example_tests/cjson_example_test.dart‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ void main() {
1414
final config = testConfigFromPath(
1515
path.join(packagePathForTests, 'example', 'c_json', 'config.yaml'),
1616
);
17-
final library = parse(testContext(config));
17+
final context = testContext(config);
18+
final library = parse(context);
1819

19-
matchLibraryWithExpected(library, 'example_c_json.dart', [
20+
matchLibraryWithExpected(context, library, 'example_c_json.dart', [
2021
config.output.dartFile.toFilePath(),
2122
]);
2223
});

‎pkgs/ffigen/test/example_tests/ffinative_example_test.dart‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ void main() {
1414
final config = testConfigFromPath(
1515
path.join(packagePathForTests, 'example', 'ffinative', 'config.yaml'),
1616
);
17-
final library = parse(testContext(config));
17+
final context = testContext(config);
18+
final library = parse(context);
1819

19-
matchLibraryWithExpected(library, 'example_ffinative.dart', [
20+
matchLibraryWithExpected(context, library, 'example_ffinative.dart', [
2021
config.output.dartFile.toFilePath(),
2122
]);
2223
});

0 commit comments

Comments
 (0)