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

Commit

Permalink
Add environment variable to override is_dev()
Browse files Browse the repository at this point in the history
Useful for unit testing software that intentionally has different
behavior in prod and dev
  • Loading branch information
fredemmott committed Aug 28, 2019
1 parent e549fd1 commit b5c0fb8
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Writer.hack
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,13 @@ function root(): string {
return $root;
}
<<__Memoize>>
function is_dev(): bool {
return $is_dev;
\$override = \getenv('HH_FORCE_IS_DEV');
if (\$override === false) {
return $is_dev;
}
return (bool) \$override;
}
function map(): \Facebook\AutoloadMap\AutoloadMap {
Expand Down
75 changes: 75 additions & 0 deletions tests/IsDevTest.hack
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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;

use function Facebook\FBExpect\expect;
use namespace HH\Lib\{Str, Vec};

final class IsDevTest extends BaseTest {
public function testIsDevInUnitTest(): void {
expect(Generated\is_dev())->toBeTrue();
}

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

$builder = (new Writer())
->setBuilder($builder)
->setRoot($root)
->setIsDev(true)
->writeToFile($tempfile);
$is_dev = self::exec(
\PHP_BINARY,
'-d',
'auto_prepend_file='.$tempfile,
$root.'/is_dev.hack',
);
expect($is_dev)->toBeSame('bool(true)');

$builder->setIsDev(false)->writeToFile($tempfile);
$is_dev = self::exec(
\PHP_BINARY,
'-d',
'auto_prepend_file='.$tempfile,
$root.'/is_dev.hack',
);
expect($is_dev)->toBeSame('bool(false)');

$is_dev = self::exec(
'/bin/sh',
'-c',
vec[
\PHP_BINARY,
'-d',
'auto_prepend_file='.$tempfile,
$root.'/is_dev.hack',
]
|> Vec\map($$, $a ==> \escapeshellarg($a))
|> Str\join($$, ' ')
|> \escapeshellcmd($$)
|> 'HH_FORCE_IS_DEV=1 '.$$
);
expect($is_dev)->toBeSame('bool(true)');

}

private static function exec(string ...$args): string {
$output = vec[];
$exit_code = -1;
Vec\map($args, $arg ==> \escapeshellarg($arg))
|> Str\join($$, ' ')
|> \exec($$, inout $output, inout $exit_code);
$output = Str\join($output, "\n");
invariant($exit_code === 0, 'exec failed: %s', $output);
return $output;
}
}
14 changes: 14 additions & 0 deletions tests/fixtures/hh-only/is_dev.hack
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* 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\Tests\IsDevEntrypoint;

<<__EntryPoint>>
function main(): void {
\var_dump(\Facebook\AutoloadMap\Generated\is_dev());
}

0 comments on commit b5c0fb8

Please sign in to comment.