|
| 1 | +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:convert'; |
| 6 | +import 'dart:io'; |
| 7 | + |
| 8 | +import 'package:code_assets/code_assets.dart'; |
| 9 | +import 'package:hooks/hooks.dart'; |
| 10 | +import 'package:native_test_helpers/native_test_helpers.dart'; |
| 11 | +import 'package:test/test.dart'; |
| 12 | + |
| 13 | +import '../hook/build.dart' as hook; |
| 14 | + |
| 15 | +void main() { |
| 16 | + test( |
| 17 | + 'build hook decodes percent-encoded package root paths', |
| 18 | + () async { |
| 19 | + final tempDir = await Directory.systemTemp.createTemp( |
| 20 | + 'objective_c_hook_path', |
| 21 | + ); |
| 22 | + addTearDown(() => tempDir.delete(recursive: true)); |
| 23 | + |
| 24 | + final realPackageRoot = findPackageRoot('objective_c'); |
| 25 | + const rootDirName = 'pkg%47root'; |
| 26 | + final rootDir = Directory('${tempDir.path}/$rootDirName'); |
| 27 | + await rootDir.create(); |
| 28 | + final symlinkPath = '${rootDir.path}/objective_c'; |
| 29 | + await Link(symlinkPath).create(realPackageRoot.toFilePath()); |
| 30 | + final symlinkDir = Directory(symlinkPath); |
| 31 | + |
| 32 | + final encodedPath = symlinkPath.replaceAll('%', '%25'); |
| 33 | + final encodedPackageRoot = Uri.parse( |
| 34 | + 'file:///${encodedPath.substring(1)}/', |
| 35 | + ); |
| 36 | + expect(encodedPackageRoot.toString(), contains('%2547')); |
| 37 | + |
| 38 | + final compilerLog = tempDir.uri.resolve('compiler_args.txt').toFilePath(); |
| 39 | + File(compilerLog).writeAsStringSync(''); |
| 40 | + final compilerScript = tempDir.uri.resolve('clang').toFilePath(); |
| 41 | + File(compilerScript).writeAsStringSync('''#!/bin/sh |
| 42 | +if [ "\$1" = "--version" ]; then |
| 43 | + echo "Apple clang version 15.0.0" |
| 44 | + exit 0 |
| 45 | +fi |
| 46 | +log="$compilerLog" |
| 47 | +out="" |
| 48 | +prev="" |
| 49 | +for arg in "\$@"; do |
| 50 | + if [ "\$prev" = "-o" ]; then |
| 51 | + out="\$arg" |
| 52 | + fi |
| 53 | + prev="\$arg" |
| 54 | + printf '%s\\n' "\$arg" >> "\$log" |
| 55 | +done |
| 56 | +if [ -n "\$out" ]; then |
| 57 | + mkdir -p "\$(dirname "\$out")" |
| 58 | + : > "\$out" |
| 59 | +fi |
| 60 | +exit 0 |
| 61 | +'''); |
| 62 | + Process.runSync('chmod', ['+x', compilerScript]); |
| 63 | + |
| 64 | + final outputDirectoryShared = tempDir.uri.resolve('output_shared/'); |
| 65 | + await Directory.fromUri(outputDirectoryShared).create(); |
| 66 | + final outputFile = tempDir.uri.resolve('output.json'); |
| 67 | + |
| 68 | + final inputBuilder = BuildInputBuilder() |
| 69 | + ..setupShared( |
| 70 | + packageRoot: encodedPackageRoot, |
| 71 | + packageName: 'objective_c', |
| 72 | + outputFile: outputFile, |
| 73 | + outputDirectoryShared: outputDirectoryShared, |
| 74 | + ) |
| 75 | + ..setupBuildInput() |
| 76 | + ..config.setupBuild(linkingEnabled: false) |
| 77 | + ..addExtension( |
| 78 | + CodeAssetExtension( |
| 79 | + targetArchitecture: Architecture.current, |
| 80 | + targetOS: OS.macOS, |
| 81 | + linkModePreference: LinkModePreference.dynamic, |
| 82 | + macOS: MacOSCodeConfig(targetVersion: 13), |
| 83 | + cCompiler: CCompilerConfig( |
| 84 | + compiler: Uri.file(compilerScript), |
| 85 | + linker: Uri.file(compilerScript), |
| 86 | + archiver: Uri.file(compilerScript), |
| 87 | + ), |
| 88 | + ), |
| 89 | + ); |
| 90 | + final input = inputBuilder.build(); |
| 91 | + final inputFile = tempDir.uri.resolve('input.json'); |
| 92 | + File.fromUri(inputFile).writeAsStringSync(json.encode(input.json)); |
| 93 | + |
| 94 | + final originalCwd = Directory.current; |
| 95 | + Directory.current = symlinkDir; |
| 96 | + try { |
| 97 | + await (hook.main as dynamic)(['--config=${inputFile.toFilePath()}']); |
| 98 | + } finally { |
| 99 | + Directory.current = originalCwd; |
| 100 | + } |
| 101 | + |
| 102 | + final logLines = File(compilerLog).readAsLinesSync(); |
| 103 | + final decodedUtil = '$symlinkPath/test/util.c'; |
| 104 | + expect(logLines, contains(decodedUtil)); |
| 105 | + expect(logLines.any((line) => line.contains('%2547')), isFalse); |
| 106 | + }, |
| 107 | + skip: !Platform.isMacOS ? 'Requires macOS' : null, |
| 108 | + ); |
| 109 | +} |
0 commit comments