Skip to content
This repository has been archived by the owner on Sep 1, 2023. It is now read-only.

Commit

Permalink
handle new XHP flags (#56)
Browse files Browse the repository at this point in the history
- enable_xhp_class_modifier
- disable_xhp_element_mangling
  • Loading branch information
jjergus authored Sep 1, 2020
1 parent 0cae9ed commit 6bd2a5a
Show file tree
Hide file tree
Showing 25 changed files with 86 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .hhconfig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
assume_php=false
enable_experimental_tc_features = no_fallback_in_namespaces, unpacking_check_arity, disallow_refs_in_array, disallow_untyped_lambda_as_non_function_type
ignored_paths = [ "vendor/.+/tests/.+", "vendor/.+/bin/.+", "tests/fixtures/.+\.tmp\..+\.hack" ]
ignored_paths = [ "vendor/.+/tests/.+", "vendor/.+/bin/.+", "testdata/fixtures/.+\.tmp\..+\.hack", "testdata/fixtures/xhp-class/.+" ]
safe_array = true
safe_vector_array = true
disallow_destruct = true
Expand Down
11 changes: 11 additions & 0 deletions .travis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,14 @@ hh_client
vendor/bin/hacktest tests/*.hack
ENABLE_HH_CLIENT_AUTOLOAD=true vendor/bin/hacktest \
tests/FallbackHandlerTest.hack

# FactParseScanner should work with any combination of enable_xhp_class_modifier
# and disable_xhp_element_mangling
for A in false true; do
for B in false true; do
hhvm \
-dhhvm.hack.lang.enable_xhp_class_modifier=$A \
-dhhvm.hack.lang.disable_xhp_element_mangling=$B \
vendor/bin/hacktest tests/ScannerTest.hack
done
done
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@

namespace Facebook\AutoloadMap\TestFixtures;

abstract class :example:xhp-class {}
abstract class :xhp-class-old {}
abstract class :xhp-namespace:xhp-class-old {}
14 changes: 14 additions & 0 deletions testdata/fixtures/xhp-class/xhp_class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?hh // strict
/*
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

namespace Facebook\AutoloadMap\TestFixtures;

abstract xhp class xhp_class_new {}
abstract xhp class xhp_namespace:xhp_class_new {}
2 changes: 1 addition & 1 deletion tests/IsDevTest.hack
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class IsDevTest extends BaseTest {
}

public function testInStandaloneExecutable(): void {
$root = __DIR__.'/fixtures/hh-only';
$root = \realpath(__DIR__.'/../testdata/fixtures/hh-only');
$builder = new RootImporter($root, IncludedRoots::DEV_AND_PROD);
$tempfile = \tempnam(\sys_get_temp_dir(), 'hh_autoload.tmp.').'.hack';

Expand Down
6 changes: 3 additions & 3 deletions tests/RootImporterTest.hack
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ final class RootImporterTest extends BaseTest {
string $test_file,
bool $relative_root,
): void {
$root = __DIR__.'/fixtures/hh-only';
$root = \realpath(__DIR__.'/../testdata/fixtures/hh-only');
$builder = new RootImporter($root, $included_roots);
$tempdir = $relative_root ? $root.'/vendor' : \sys_get_temp_dir();
$tempfile = \tempnam($tempdir, 'hh_autoload.tmp.').'.hack';
Expand All @@ -60,7 +60,7 @@ final class RootImporterTest extends BaseTest {
\PHP_BINARY,
'-v',
'Eval.Jit=0',
__DIR__.'/fixtures/hh-only/'.$test_file,
\realpath(__DIR__.'/../testdata/fixtures/hh-only/'.$test_file),
$tempfile,
],
$x ==> \escapeshellarg($x),
Expand Down Expand Up @@ -89,7 +89,7 @@ final class RootImporterTest extends BaseTest {
// If a project uses <= 1.3, their existing composer plugin will try to do
// this while upgrading, and error out if it fails - so, we need to keep
// suppporting it.
$root = __DIR__.'/fixtures/hh-only';
$root = \realpath(__DIR__.'/../testdata/fixtures/hh-only');
$builder = new RootImporter($root);
expect($builder)->toNotBeNull();
}
Expand Down
62 changes: 54 additions & 8 deletions tests/ScannerTest.hack
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,43 @@

namespace Facebook\AutoloadMap;

use namespace HH\Lib\Dict;
use type Facebook\HackTest\DataProvider;
use function Facebook\FBExpect\expect;

final class ScannerTest extends BaseTest {
const string FIXTURES = __DIR__.'/fixtures';
const string FIXTURES = __DIR__.'/../testdata/fixtures';
const string HH_ONLY_SRC = self::FIXTURES.'/hh-only/src';
const string XHP_CLASS_SRC = self::FIXTURES.'/xhp-class';
const string FIXTURES_PREFIX = "Facebook\\AutoloadMap\\TestFixtures\\";

<<DataProvider('getParsers')>>
public function testHHOnly(Parser $parser): void {
$map = Scanner::fromTree(self::HH_ONLY_SRC, $parser)->getAutoloadMap();

// Some of the names returned by FactParseScanner are invalid, but we only
// care about the valid ones being parsed correctly.
if (\ini_get('hhvm.hack.lang.disable_xhp_element_mangling')) {
$xhp_classes = keyset[
'xhp-class-old',
'xhp-namespace\\xhp-class-old',
];
} else {
$xhp_classes = keyset[
'xhp_xhp_class_old',
'xhp_xhp_namespace__xhp_class_old',
];
}

$this->assertMapMatches(
dict[
'ExampleClassInHH' => 'class_in_hh.hh',
'ExampleClass' => 'class.php',
'ExampleEnum' => 'enum.php',
'xhp_example__xhp_class' => 'xhp_class.php',
],
Dict\merge(
dict[
'ExampleClassInHH' => 'class_in_hh.hh',
'ExampleClass' => 'class.php',
'ExampleEnum' => 'enum.php',
],
Dict\from_keys($xhp_classes, $_ ==> 'xhp_class.php'),
),
$map['class'],
);

Expand All @@ -53,6 +71,33 @@ final class ScannerTest extends BaseTest {
);
}

<<DataProvider('getParsers')>>
public function testNewXHPClassSyntax(Parser $parser): void {
if (!\ini_get('hhvm.hack.lang.enable_xhp_class_modifier')) {
self::markTestSkipped('requires enable_xhp_class_modifier=true');
}

$map = Scanner::fromTree(self::XHP_CLASS_SRC, $parser)->getAutoloadMap();

if (\ini_get('hhvm.hack.lang.disable_xhp_element_mangling')) {
$xhp_classes = keyset[
'xhp_class_new',
'xhp_namespace\\xhp_class_new',
];
} else {
$xhp_classes = keyset[
'xhp_class_new',
'xhp_namespace__xhp_class_new',
];
}

$this->assertMapMatches(
Dict\from_keys($xhp_classes, $_ ==> 'xhp_class.php'),
$map['class'],
self::XHP_CLASS_SRC,
);
}

<<DataProvider('getParsers')>>
public function testFromTree(
Parser $parser,
Expand Down Expand Up @@ -85,9 +130,10 @@ final class ScannerTest extends BaseTest {
private function assertMapMatches(
dict<string, string> $expected,
dict<string, string> $actual,
string $path_prefix = self::HH_ONLY_SRC,
): void {
foreach ($expected as $name => $file) {
$a = self::HH_ONLY_SRC.'/'.$file;
$a = $path_prefix.'/'.$file;
$b = idx($actual, \strtolower(self::FIXTURES_PREFIX.$name)) ??
idx($actual, self::FIXTURES_PREFIX.$name) ??
idx($actual, $name);
Expand Down

0 comments on commit 6bd2a5a

Please sign in to comment.