Initial download tests for extension and unique filename
This commit is contained in:
parent
7d5fd0d319
commit
09432b2d12
2 changed files with 65 additions and 3 deletions
|
@ -228,7 +228,7 @@ namespace Midori {
|
||||||
return filename;
|
return filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string? get_extension_for_uri (string uri, out string basename) {
|
public static string? get_extension_for_uri (string uri, out string basename = null) {
|
||||||
if (&basename != null)
|
if (&basename != null)
|
||||||
basename = null;
|
basename = null;
|
||||||
/* Find the last slash and the last period *after* the last slash. */
|
/* Find the last slash and the last period *after* the last slash. */
|
||||||
|
@ -248,11 +248,11 @@ namespace Midori {
|
||||||
}
|
}
|
||||||
|
|
||||||
public string get_unique_filename (string filename) {
|
public string get_unique_filename (string filename) {
|
||||||
if (Posix.access (filename, Posix.F_OK) != 0) {
|
if (Posix.access (filename, Posix.F_OK) == 0) {
|
||||||
string basename;
|
string basename;
|
||||||
string? extension = get_extension_for_uri (filename, out basename);
|
string? extension = get_extension_for_uri (filename, out basename);
|
||||||
string? new_filename = null;
|
string? new_filename = null;
|
||||||
int i = -1;
|
int i = 0;
|
||||||
do {
|
do {
|
||||||
new_filename = "%s-%d%s".printf (basename, i++, extension ?? "");
|
new_filename = "%s-%d%s".printf (basename, i++, extension ?? "");
|
||||||
} while (Posix.access (new_filename, Posix.F_OK) == 0);
|
} while (Posix.access (new_filename, Posix.F_OK) == 0);
|
||||||
|
|
62
tests/download.vala
Normal file
62
tests/download.vala
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
/*
|
||||||
|
Copyright (C) 2012 Christian Dywan <christian@twotoasts.de>
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 2.1 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
See the file COPYING for the full license text.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Katze {
|
||||||
|
extern static string assert_str_equal (string input, string result, string expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct TestCase {
|
||||||
|
public string data;
|
||||||
|
public string? expected;
|
||||||
|
}
|
||||||
|
|
||||||
|
const TestCase[] filenames = {
|
||||||
|
{ "/tmp/midori-user/tumblr123.jpg", ".jpg" },
|
||||||
|
{ "https://green.cat/8019B6/a.b/500.jpg", ".jpg" },
|
||||||
|
{ "http://example.com/file.png", ".png" }
|
||||||
|
};
|
||||||
|
|
||||||
|
static void download_extension () {
|
||||||
|
foreach (var filename in filenames) {
|
||||||
|
string? result = Midori.Download.get_extension_for_uri (filename.data);
|
||||||
|
Katze.assert_str_equal (filename.data, result, filename.expected);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void download_unique () {
|
||||||
|
string folder = DirUtils.make_tmp ("cacheXXXXXX");
|
||||||
|
string filename = Path.build_path (Path.DIR_SEPARATOR_S, folder, "foo.png");
|
||||||
|
string unique = Midori.Download.get_unique_filename (filename);
|
||||||
|
Katze.assert_str_equal (folder, unique, filename);
|
||||||
|
FileUtils.set_contents (filename, "12345");
|
||||||
|
unique = Midori.Download.get_unique_filename (filename);
|
||||||
|
filename = Path.build_path (Path.DIR_SEPARATOR_S, folder, "foo-0.png");
|
||||||
|
Katze.assert_str_equal (folder, unique, filename);
|
||||||
|
FileUtils.set_contents (filename, "12345");
|
||||||
|
unique = Midori.Download.get_unique_filename (filename);
|
||||||
|
filename = Path.build_path (Path.DIR_SEPARATOR_S, folder, "foo-1.png");
|
||||||
|
Katze.assert_str_equal (folder, unique, filename);
|
||||||
|
|
||||||
|
filename = Path.build_path (Path.DIR_SEPARATOR_S, folder, "foo-9.png");
|
||||||
|
FileUtils.set_contents (filename, "12345");
|
||||||
|
unique = Midori.Download.get_unique_filename (filename);
|
||||||
|
filename = Path.build_path (Path.DIR_SEPARATOR_S, folder, "foo-10.png");
|
||||||
|
Katze.assert_str_equal (folder, unique, filename);
|
||||||
|
DirUtils.remove (folder);
|
||||||
|
}
|
||||||
|
|
||||||
|
void main (string[] args) {
|
||||||
|
Test.init (ref args);
|
||||||
|
Test.add_func ("/download/extension", download_extension);
|
||||||
|
Test.add_func ("/download/unique", download_unique);
|
||||||
|
Test.run ();
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue