Address better renaming review comments and suggestions pt.1

pull/709/head
Nils Maier 2016-07-19 15:58:36 +02:00
parent e6e2de96ca
commit 67f556f208
2 changed files with 47 additions and 8 deletions

View File

@ -780,9 +780,21 @@ void RequestGroup::tryAutoFileRenaming()
}
auto fn = filepath;
std::string ext;
auto idx = fn.find_last_of(".");
auto slash = fn.find_last_of("\\/");
if (idx != std::string::npos && (slash == std::string::npos || slash < idx)) {
const auto idx = fn.find_last_of(".");
const auto slash = fn.find_last_of("\\/");
// Do extract the extension, as in "file.ext" = "file" and ".ext",
// but do not consider ".file" to be a file name without extension instead
// of a blank file name and an extension of ".file"
if (idx != std::string::npos &&
// fn has no path component and starts with a dot, but has no extension
// otherwise
idx != 0 &&
// has a file path component if we found a slash.
// if slash == idx - 1 this means a form of "*/.*", so the file name
// starts with a dot, has no extension otherwise, and therefore do not
// extract an extension either
(slash == std::string::npos || slash < idx - 1)
) {
ext = fn.substr(idx);
fn = fn.substr(0, idx);
}

View File

@ -14,6 +14,7 @@ class RequestGroupTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(RequestGroupTest);
CPPUNIT_TEST(testGetFirstFilePath);
CPPUNIT_TEST(testTryAutoFileRenaming);
CPPUNIT_TEST(testCreateDownloadResult);
CPPUNIT_TEST_SUITE_END();
@ -24,6 +25,7 @@ public:
void setUp() { option_.reset(new Option()); }
void testGetFirstFilePath();
void testTryAutoFileRenaming();
void testCreateDownloadResult();
};
@ -39,7 +41,22 @@ void RequestGroupTest::testGetFirstFilePath()
CPPUNIT_ASSERT_EQUAL(std::string("/tmp/myfile"), group.getFirstFilePath());
// test file renaming
// test in-memory
ctx->getFirstFileEntry()->setPath("/tmp/myfile");
group.markInMemoryDownload();
CPPUNIT_ASSERT_EQUAL(std::string("[MEMORY]myfile"), group.getFirstFilePath());
}
void RequestGroupTest::testTryAutoFileRenaming()
{
std::shared_ptr<DownloadContext> ctx(
new DownloadContext(1_k, 1_k, "/tmp/myfile"));
RequestGroup group(GroupId::create(), option_);
group.setDownloadContext(ctx);
option_->put(PREF_AUTO_FILE_RENAMING, "false");
try {
group.tryAutoFileRenaming();
@ -65,12 +82,22 @@ void RequestGroupTest::testGetFirstFilePath()
group.tryAutoFileRenaming();
CPPUNIT_ASSERT_EQUAL(std::string("/tmp.txt/myfile.1.txt"), group.getFirstFilePath());
// test in-memory
ctx->getFirstFileEntry()->setPath("/tmp/myfile");
ctx->getFirstFileEntry()->setPath(".bashrc");
group.tryAutoFileRenaming();
CPPUNIT_ASSERT_EQUAL(std::string(".bashrc.1"), group.getFirstFilePath());
group.markInMemoryDownload();
ctx->getFirstFileEntry()->setPath(".bashrc.txt");
group.tryAutoFileRenaming();
CPPUNIT_ASSERT_EQUAL(std::string(".bashrc.1.txt"), group.getFirstFilePath());
ctx->getFirstFileEntry()->setPath("/tmp.txt/.bashrc");
group.tryAutoFileRenaming();
CPPUNIT_ASSERT_EQUAL(std::string("/tmp.txt/.bashrc.1"), group.getFirstFilePath());
ctx->getFirstFileEntry()->setPath("/tmp.txt/.bashrc.txt");
group.tryAutoFileRenaming();
CPPUNIT_ASSERT_EQUAL(std::string("/tmp.txt/.bashrc.1.txt"), group.getFirstFilePath());
CPPUNIT_ASSERT_EQUAL(std::string("[MEMORY]myfile"), group.getFirstFilePath());
}
void RequestGroupTest::testCreateDownloadResult()