diff --git a/.gitignore b/.gitignore index c6330ed..b52041a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.swp /vendor/ +hh_autoload.tmp.* diff --git a/src/Writer.hack b/src/Writer.hack index 85869be..2d16b26 100644 --- a/src/Writer.hack +++ b/src/Writer.hack @@ -9,6 +9,8 @@ namespace Facebook\AutoloadMap; +use namespace HH\Lib\Str; + /** Class to write `autoload.hack`. * * This includes: @@ -181,8 +183,13 @@ final class Writer { |> \str_replace(')', ']', $$); if ($this->relativeAutoloadRoot) { - $autoload_map_typedef = - '__DIR__.\'/../\'.'.\var_export($this->relativePath(__DIR__.'/AutoloadMap.hack'), true); + try { + $autoload_map_typedef = + '__DIR__.\'/../'.\var_export($this->relativePath(__DIR__.'/AutoloadMap.hack'), true); + } catch (\Exception $_) { + // Our unit tests need to load it, and are rooted in the tests/ subdir + $autoload_map_typedef = \var_export(__DIR__.'/AutoloadMap.hack', true); + } } else { $autoload_map_typedef = \var_export(__DIR__.'/AutoloadMap.hack', true); } @@ -254,13 +261,13 @@ EOF; throw new Exception('Call setRoot() before writeToFile()'); } $path = \realpath($path); - if (\strpos($path, $root) !== 0) { - throw new Exception( - "%s is outside root %s", - $path, - $root, - ); + if (Str\starts_with($path, $root)) { + return Str\slice($path, Str\length($root) + 1); } - return \substr($path, \strlen($root) + 1); + throw new Exception( + "%s is outside root %s", + $path, + $root, + ); } } diff --git a/tests/ComposerImporterTest.php b/tests/ComposerImporterTest.php index 7fdb12f..8396261 100644 --- a/tests/ComposerImporterTest.php +++ b/tests/ComposerImporterTest.php @@ -32,17 +32,6 @@ public function testRootImportWithScannedFiles(Parser $parser): void { ), ); expect($importer->getFiles())->toBeEmpty(); - - $map = $importer->getAutoloadMap(); - expect(idx($map['class'], 'facebook\autoloadmap\exception'))->toBeSame( - $root.'/src/Exception.php', - ); - expect(idx($map['class'], 'facebook\autoloadmap\writer'))->toBeSame( - $root.'/src/Writer.php', - ); - expect(idx($map['type'], 'facebook\autoloadmap\config'))->toBeSame( - $root.'/src/Config.php', - ); } <> @@ -65,7 +54,7 @@ public function testRootImportWithRequiredFiles(Parser $parser): void { $map = $importer->getAutoloadMap(); expect($map['type'])->toBeEmpty(); - expect($importer->getFiles())->toContain($root.'/src/AutoloadMap.php'); + expect($importer->getFiles())->toContain($root.'/src/ComposerPlugin.php'); } <> diff --git a/tests/RootImporterTest.php b/tests/RootImporterTest.php index f7f113c..0d66ab3 100644 --- a/tests/RootImporterTest.php +++ b/tests/RootImporterTest.php @@ -46,7 +46,7 @@ public function testImportTree( $root = __DIR__.'/fixtures/hh-only'; $builder = new RootImporter($root, $included_roots); $tempdir = $relative_root ? $root.'/vendor' : \sys_get_temp_dir(); - $tempfile = \tempnam($tempdir, 'hh_autoload'); + $tempfile = \tempnam($tempdir, 'hh_autoload.tmp.').'.hack'; (new Writer()) ->setBuilder($builder) ->setRoot($root) diff --git a/tests/fixtures/hh-only/test-dev.php b/tests/fixtures/hh-only/test-dev.php index 456d8c0..9ba15be 100644 --- a/tests/fixtures/hh-only/test-dev.php +++ b/tests/fixtures/hh-only/test-dev.php @@ -11,6 +11,7 @@ namespace Facebook\AutoloadMap\TestFixtures; require($argv[1]); +\Facebook\AutoloadMap\initialize(); $x = new ExampleClass(); example_function(); diff --git a/tests/fixtures/hh-only/test-prod.php b/tests/fixtures/hh-only/test-prod.php index beeee0a..5bc9a15 100644 --- a/tests/fixtures/hh-only/test-prod.php +++ b/tests/fixtures/hh-only/test-prod.php @@ -11,6 +11,9 @@ namespace Facebook\AutoloadMap\TestFixtures; require($argv[1]); +\var_dump($argv[1]); +\var_dump(\file_get_contents($argv[1])); +\Facebook\AutoloadMap\initialize(); $x = new ExampleClass(); example_function();