function testChdirRelativeRoot() { $dir = new WatchmanDirectoryFixture(); $log = $dir->getPath() . "log"; $env = $dir->getPath() . "env"; $root = realpath($dir->getPath()) . "/dir"; mkdir($root); mkdir("{$root}/sub1"); mkdir("{$root}/sub1/sub2"); $this->watch($root); $this->trigger($root, array('name' => 'cap', 'command' => array(PHP_BINARY, '-d variables_order=EGPCS', '-d register_argc_argv=1', dirname(__FILE__) . DIRECTORY_SEPARATOR . '_capture.php', $log, $env), 'expression' => array('suffix', 'txt'), 'stdin' => array('name'), 'relative_root' => 'sub1', 'chdir' => 'sub2')); touch("{$root}/sub1/A.txt"); $obj = $this->waitForJsonInput($log); $this->assertEqual(1, count($obj)); $root_pat = preg_quote(w_normalize_filename($root) . DIRECTORY_SEPARATOR . 'sub1' . DIRECTORY_SEPARATOR . 'sub2'); $this->waitFor(function () use($env, $root, $root_pat) { $envdata = @file_get_contents($env); $root = preg_quote($root); return preg_match(",PWD={$root_pat},i", $envdata) == 1; }, 10, function () use($env, $root, $root_pat) { $envdata = @file_get_contents($env); return "{$envdata}\nwaiting for PWD to show in {$env} log file " . "(pat: {$root_pat})"; }); $envdata = file_get_contents($env); $sub1_pat = preg_quote(w_normalize_filename("{$root}/sub1")); $root_pat = preg_quote(w_normalize_filename($root)); $this->assertRegex(",PWD={$root_pat},i", $envdata); $this->assertRegex("/WATCHMAN_EMPTY_ENV_VAR=\$/m", $envdata); $this->assertRegex(",^WATCHMAN_ROOT={$root_pat}\$,mi", $envdata); $this->assertRegex(",^WATCHMAN_RELATIVE_ROOT={$sub1_pat}\$,mi", $envdata); }
public function getPath($to_file = null) { if ($to_file) { return $this->path . DIRECTORY_SEPARATOR . ltrim(w_normalize_filename($to_file, "\\/")); } return $this->path; }
function testTriggerRelativeRoot() { $dir = new WatchmanDirectoryFixture(); $log = $dir->getPath("log"); $env = $dir->getPath("env"); $root = $dir->getPath("dir"); mkdir($root); mkdir("{$root}/subdir"); $this->watch($root); // The command also xtests and prints out the cwd that the bash process was // invoked with, to make sure that the cwd is the subdirectory. $this->trigger($root, array('name' => 'pwd cat', 'command' => array(PHP_BINARY, '-d register_argc_argv=1', dirname(__FILE__) . DIRECTORY_SEPARATOR . '_capture.php', $log, $env), 'relative_root' => 'subdir', 'expression' => array('suffix', 'txt'), 'stdin' => 'NAME_PER_LINE')); touch("{$root}/A.txt"); touch("{$root}/subdir/B.txt"); $root_pat = preg_quote(w_normalize_filename($root) . DIRECTORY_SEPARATOR . 'subdir'); $this->waitFor(function () use($env, $root, $root_pat) { $envdata = @file_get_contents($env); return preg_match(",PWD={$root_pat},i", $envdata) > 0; }, 10, function () use($env, $root_pat) { $envdata = @file_get_contents($env); return $envdata . "\n" . "waiting for PWD to show in {$env} log file ({$root_pat})"; }); $envdata = file_get_contents($env); $this->assertRegex(",PWD={$root_pat},i", $envdata); $this->assertFileContents($log, "B.txt\n\n"); }
function testReuseNestedWatch() { $dir = new WatchmanDirectoryFixture(); $root = realpath($dir->getPath()); mkdir("{$root}/a/b/c", 0777, true); touch("{$root}/a/b/c/.watchmanconfig"); $res = $this->watchProject($root); $this->assertEqual($root, idx($res, 'watch')); $res = $this->watchProject("{$root}/a/b/c"); $this->assertEqual($root, idx($res, 'watch'), 'watch other root'); $this->assertEqual(w_normalize_filename('a/b/c'), idx($res, 'relative_path'), 'should re-use other watch'); }
function testImmediateSubscribe() { $dir = new WatchmanDirectoryFixture(); $root = realpath($dir->getPath()); mkdir("{$root}/.hg"); $this->watch($root); $this->assertFileList($root, array('.hg')); try { $sub = $this->watchmanCommand('subscribe', $root, 'nodefer', array('fields' => array('name', 'exists'), 'defer_vcs' => false)); $this->waitForSub('nodefer', function ($data) { return true; }); list($sub) = $this->getSubData('nodefer'); $this->assertEqual(true, $sub['is_fresh_instance']); $files = $sub['files']; $this->assertEqual(array(array('name' => '.hg', 'exists' => true)), $files); touch("{$root}/.hg/wlock"); $this->waitForSub('nodefer', function ($data) { return true; }); $sub = $this->tail($this->getSubData('nodefer')); $wlock = null; foreach ($sub['files'] as $ent) { if ($ent['name'] == '.hg/wlock') { $wlock = $ent; } } $this->assertEqual(array('name' => w_normalize_filename('.hg/wlock'), 'exists' => true), $ent); unlink("{$root}/.hg/wlock"); $this->waitForSub('nodefer', function ($data) { return true; }); $sub = $this->tail($this->getSubData('nodefer')); $wlock = null; foreach ($sub['files'] as $ent) { if ($ent['name'] == '.hg/wlock') { $wlock = $ent; } } $this->assertEqual(array('name' => w_normalize_filename('.hg/wlock'), 'exists' => false), $ent); $this->watchmanCommand('unsubscribe', $root, 'nodefer'); } catch (Exception $e) { $this->watchmanCommand('unsubscribe', $root, 'nodefer'); throw $e; } }
function testTypeExpr() { $dir = new WatchmanDirectoryFixture(); $root = $dir->getPath(); touch("{$root}/foo.c"); mkdir("{$root}/subdir"); touch("{$root}/subdir/bar.txt"); $this->watch($root); $res = $this->watchmanCommand('query', $root, array('expression' => array('type', 'f'), 'fields' => array('name'))); $files = $res['files']; sort($files); $this->assertEqualFileList(array('foo.c', 'subdir/bar.txt'), $files); $res = $this->watchmanCommand('query', $root, array('expression' => array('type', 'd'), 'fields' => array('name', 'type'))); $this->assertEqual(array(array('name' => 'subdir', 'type' => 'd')), $res['files']); $res = $this->watchmanCommand('query', $root, array('expression' => array('type', 'f'), 'fields' => array('name', 'type'))); usort($res['files'], function ($a, $b) { return strcmp($a['name'], $b['name']); }); $this->assertEqual(array(array('name' => 'foo.c', 'type' => 'f'), array('name' => w_normalize_filename('subdir/bar.txt'), 'type' => 'f')), $res['files']); $res = $this->watchmanCommand('query', $root, array('expression' => array('type', 'x'), 'fields' => array('name'))); $this->assertEqual("failed to parse query: invalid type string 'x'", $res['error']); $res = $this->watchmanCommand('query', $root, array('expression' => 'type', 'fields' => array('name'))); $this->assertEqual('failed to parse query: must use ["type", "typestr"]', $res['error']); }
function watch($root, $assert = true) { $root = w_normalize_filename($root); $res = $this->watchmanCommand('watch', $root); $this->watches[$root] = $res; if ($assert) { if (!is_array($res)) { $err = $res; } else { $err = idx($res, 'error', idx($res, 'watch')); } $this->assertEqual($root, $err); } return $res; }