|
| 1 | +import test from 'node:test' |
| 2 | +import assert from 'node:assert/strict' |
| 3 | +import { mkdtempSync, readFileSync, writeFileSync } from 'node:fs' |
| 4 | +import { spawnSync } from 'node:child_process' |
| 5 | +import { tmpdir } from 'node:os' |
| 6 | +import path from 'node:path' |
| 7 | + |
| 8 | +const compiler = ['clang++-18', 'clang++', 'c++'].find(command => |
| 9 | + spawnSync(command, ['--version'], { stdio: 'ignore' }).status === 0) |
| 10 | + |
| 11 | +test('Android packaged files are not treated as directories', { |
| 12 | + skip: !compiler ? 'A C++ compiler is required' : false |
| 13 | +}, () => { |
| 14 | + const directory = mkdtempSync(path.join(tmpdir(), 'oro-android-asset-type-')) |
| 15 | + const source = readFileSync(new URL('../../src/runtime/filesystem/resource.cc', import.meta.url), 'utf8') |
| 16 | + const start = source.indexOf(' bool Resource::isFile (const Path& resourcePath) {') |
| 17 | + const end = source.indexOf(' bool Resource::isMountedPath (', start) |
| 18 | + assert.ok(start >= 0 && end > start) |
| 19 | + |
| 20 | + // Compile the production predicates with the NDK's asset-handle behavior: |
| 21 | + // openDir returns an allocated handle even when its argument names a file. |
| 22 | + const fixture = ` |
| 23 | + #include <cassert> |
| 24 | + #include <filesystem> |
| 25 | + #include <fstream> |
| 26 | + #include <string> |
| 27 | + #define ORO_RUNTIME_PLATFORM_ANDROID 1 |
| 28 | + namespace fs = std::filesystem; |
| 29 | + using Path = fs::path; |
| 30 | + using String = std::string; |
| 31 | + struct AAssetManager {}; |
| 32 | + struct AAsset {}; |
| 33 | + struct AAssetDir {}; |
| 34 | + constexpr int AASSET_MODE_BUFFER = 3; |
| 35 | + AAssetManager manager; |
| 36 | + AAssetManager* sharedAndroidAssetManager = &manager; |
| 37 | + int openAssets = 0; |
| 38 | + int openDirectories = 0; |
| 39 | + AAsset* AAssetManager_open (AAssetManager*, const char* name, int) { |
| 40 | + if (String(name) == "index.html" || String(name) == "oro/test.js") { |
| 41 | + ++openAssets; |
| 42 | + return new AAsset; |
| 43 | + } |
| 44 | + return nullptr; |
| 45 | + } |
| 46 | + void AAsset_close (AAsset* asset) { --openAssets; delete asset; } |
| 47 | + AAssetDir* AAssetManager_openDir (AAssetManager*, const char*) { |
| 48 | + ++openDirectories; |
| 49 | + return new AAssetDir; |
| 50 | + } |
| 51 | + void AAssetDir_close (AAssetDir* directory) { --openDirectories; delete directory; } |
| 52 | + Path getRelativeAndroidAssetManagerPath (const Path& resourcePath) { return resourcePath; } |
| 53 | + struct Resource { |
| 54 | + static bool isFile (const Path&); |
| 55 | + static bool isDirectory (const Path&); |
| 56 | + static bool isDirectory (const String&); |
| 57 | + }; |
| 58 | + ${source.slice(start, end)} |
| 59 | + int main () { |
| 60 | + assert(Resource::isFile(Path("index.html"))); |
| 61 | + assert(!Resource::isDirectory(Path("index.html"))); |
| 62 | + assert(!Resource::isDirectory(Path("oro/test.js"))); |
| 63 | + assert(Resource::isDirectory(Path("oro"))); |
| 64 | + assert(Resource::isDirectory(Path(""))); |
| 65 | + std::ofstream("loose.txt") << "fixture"; |
| 66 | + assert(!Resource::isDirectory(Path("loose.txt"))); |
| 67 | + sharedAndroidAssetManager = nullptr; |
| 68 | + assert(Resource::isDirectory(Path("."))); |
| 69 | + assert(!Resource::isDirectory(Path("missing"))); |
| 70 | + assert(openAssets == 0); |
| 71 | + assert(openDirectories == 0); |
| 72 | + } |
| 73 | + ` |
| 74 | + const filename = path.join(directory, 'asset-type.cc') |
| 75 | + const executable = path.join(directory, process.platform === 'win32' ? 'asset-type.exe' : 'asset-type') |
| 76 | + writeFileSync(filename, fixture) |
| 77 | + const compiled = spawnSync(compiler, ['-std=c++20', filename, '-o', executable], { encoding: 'utf8' }) |
| 78 | + assert.equal(compiled.status, 0, compiled.stderr) |
| 79 | + const result = spawnSync(executable, [], { cwd: directory, encoding: 'utf8' }) |
| 80 | + assert.equal(result.status, 0, result.stderr) |
| 81 | +}) |
0 commit comments