more thorough testing for path_to_tags, and handle more edge cases

This commit is contained in:
Shish 2022-10-27 16:56:12 +01:00
parent bf03c0849a
commit a828c3e0e5
3 changed files with 45 additions and 5 deletions

View file

@ -20,9 +20,6 @@ function add_dir(string $base): array
$filename = basename($full_path);
$tags = path_to_tags($short_path);
if ($tags[0] == "\\") {
$tags = "";
}
$result = "$short_path (".str_replace(" ", ", ", $tags).")... ";
try {
add_image($full_path, $filename, $tags);

View file

@ -85,4 +85,44 @@ class UtilTest extends TestCase
load_balance_url("https://{foo=10,bar=5,baz=5}.mycdn.com/$hash.$ext", $hash, 1)
);
}
public function test_path_to_tags()
{
$this->assertEquals(
"",
path_to_tags("nope.jpg")
);
$this->assertEquals(
"",
path_to_tags("\\")
);
$this->assertEquals(
"",
path_to_tags("/")
);
$this->assertEquals(
"",
path_to_tags("C:\\")
);
$this->assertEquals(
"test tag",
path_to_tags("123 - test tag.jpg")
);
$this->assertEquals(
"foo bar",
path_to_tags("/foo/bar/baz.jpg")
);
$this->assertEquals(
"cake pie foo bar",
path_to_tags("/foo/bar/123 - cake pie.jpg")
);
$this->assertEquals(
"bacon lemon",
path_to_tags("\\bacon\\lemon\\baz.jpg")
);
$this->assertEquals(
"category:tag",
path_to_tags("/category:/tag/baz.jpg")
);
}
}

View file

@ -354,10 +354,13 @@ function path_to_tags(string $path): string
$tags = explode(" ", $matches[1]);
}
$path = dirname($path);
$path = str_replace("\\", "/", $path);
$path = str_replace(";", ":", $path);
$path = str_replace("__", " ", $path);
$path = dirname($path);
if ($path == "\\" || $path == "/" || $path == ".") {
$path = "";
}
$category = "";
foreach (explode("/", $path) as $dir) {