Files
fbshipit/tests/PathFiltersTest.php
Fred Emmott 6199b1ccc3 add system for removing files outside of whitelised dirs
Summary: Really annoying for a project that has multiple source roots.

Reviewed By: JoelMarcey

Differential Revision: D3123676

fb-gh-sync-id: 3b55e5b6eab54aa4ed7a3d396f50bd97ea41d993
fbshipit-source-id: 3b55e5b6eab54aa4ed7a3d396f50bd97ea41d993
2016-04-04 16:35:33 -07:00

190 lines
5.3 KiB
PHP

<?hh
/**
* Copyright (c) 2016-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
namespace Facebook\ShipIt;
final class PathFiltersTest extends BaseTest {
public function stripPathsTestData(
): array<string, (
ImmVector<string>, // $patterns,
ImmVector<string>, // $exceptions,
ImmSet<string>, // $expected_files,
)> {
return [
'No change' => tuple(
ImmVector {},
ImmVector {},
ImmSet { 'foo', 'bar', 'herp/derp', 'derp' },
),
'Remove top level file' => tuple(
ImmVector { '@^bar$@' },
ImmVector {},
ImmSet { 'foo', 'herp/derp', 'derp' },
),
'Remove directory ' => tuple(
ImmVector { '@^herp/@' },
ImmVector { },
ImmSet { 'foo', 'bar', 'derp' },
),
'Remove file' => tuple (
ImmVector { '@(^|/)derp(/|$)@' },
ImmVector { },
ImmSet { 'foo', 'bar' },
),
'Remove file, except if parent directory has specific name' => tuple(
ImmVector { '@(^|/)derp(/|$)@' },
ImmVector { '@(^|/)herp/derp$@' },
ImmSet { 'foo', 'bar', 'herp/derp' },
),
'Multiple patterns' => tuple(
ImmVector { '@^foo$@', '@^bar$@' },
ImmVector {},
ImmSet { 'herp/derp', 'derp' },
),
'Multiple exceptions' => tuple(
ImmVector { '@@' },
ImmVector { '@foo@', '@bar@' },
ImmSet { 'foo', 'bar' },
),
];
}
/**
* @dataProvider stripPathsTestData
*/
public function testStripPaths(
ImmVector<string> $patterns,
ImmVector<string> $exceptions,
ImmSet<string> $expected_files,
): void {
$changeset = (new ShipItChangeset())->withDiffs(
self::diffsFromMap(ImmMap {
'foo' => 'placeholder',
'bar' => 'placeholder',
'herp/derp' => 'placeholder',
'derp' => 'placeholder',
}),
);
$changeset = ShipItPathFilters::stripPaths(
$changeset,
$patterns,
$exceptions,
);
$this->assertEquals(
$expected_files,
$changeset->getDiffs()->map($diff ==> $diff['path'])->toImmSet(),
);
}
public function examplesForMoveDirectories(
): array<
string,
(ImmMap<string,string>, ImmVector<string>, ImmVector<string>)
> {
return [
'first takes precedence (first is more specific)' => tuple(
ImmMap {
'foo/public_tld/' => '',
'foo/' => ''
},
ImmVector { 'foo/orig_root_file', 'foo/public_tld/public_root_file' },
ImmVector { 'orig_root_file', 'public_root_file' },
),
// this mapping doesn't make sense given the behavior, just using it to
// check that order matters
'first takes precedence (second is more specific)' => tuple(
ImmMap {
'foo/' => '',
'foo/public_tld/' => '',
},
ImmVector { 'foo/orig_root_file', 'foo/public_tld/public_root_file' },
ImmVector { 'orig_root_file', 'public_tld/public_root_file' },
),
'only one rule applied' => tuple(
ImmMap {
'foo/' => '',
'bar/' => 'project_bar/',
},
ImmVector { 'foo/bar/part of project foo', 'bar/part of project bar' },
ImmVector {
'bar/part of project foo',
'project_bar/part of project bar',
},
),
];
}
/**
* @dataProvider examplesForMoveDirectories
*/
public function testMoveDirectories(
ImmMap<string, string> $map,
ImmVector<string> $in,
ImmVector<string> $expected,
): void {
$changeset = (new ShipItChangeset())
->withDiffs($in->map($path ==> shape('path' => $path, 'body' => 'junk')));
$changeset = ShipItPathFilters::moveDirectories($changeset, $map);
$this->assertEquals(
$expected->toArray(),
$changeset->getDiffs()->map($diff ==> $diff['path'])->toArray(),
);
}
public function examplesForStripExceptDirectories(
): array<(ImmSet<string>, ImmVector<string>, ImmVector<string>)> {
return [
tuple(
ImmSet { 'foo' },
ImmVector { 'foo/bar', 'herp/derp' },
ImmVector { 'foo/bar' },
),
tuple(
ImmSet { 'foo/' },
ImmVector { 'foo/bar', 'herp/derp' },
ImmVector { 'foo/bar' },
),
tuple(
ImmSet { 'foo' },
ImmVector { 'foo/bar', 'foobaz' },
ImmVector { 'foo/bar' },
),
tuple(
ImmSet { 'foo', 'herp' },
ImmVector { 'foo/bar', 'herp/derp', 'baz' },
ImmVector { 'foo/bar', 'herp/derp' },
),
];
}
/**
* @dataProvider examplesForStripExceptDirectories
*/
public function testStripExceptDirectories(
ImmSet<string> $roots,
ImmVector<string> $paths_in,
ImmVector<string> $paths_expected,
): void {
$changeset = (new ShipItChangeset())
->withDiffs($paths_in->map(
$path ==> shape('path' => $path, 'body' => 'junk')
));
$changeset = ShipItPathFilters::stripExceptDirectories(
$changeset,
$roots,
);
$this->assertEquals(
$paths_expected->toArray(),
$changeset->getDiffs()->map($diff ==> $diff['path'])->toArray(),
);
}
}