From: Ramakrishnan Muthukrishnan <ram@leastauthority.com>
Date: Mon, 31 Aug 2015 07:32:34 +0000 (+0530)
Subject: WIP: test for _write_downloaded_file
X-Git-Url: https://git.rkrishnan.org/specifications/%5B/%5D%20/%22doc.html/index.php?a=commitdiff_plain;h=c224c6a996a41248e54bd0f3b380a0174c519842;p=tahoe-lafs%2Ftahoe-lafs.git

WIP: test for _write_downloaded_file
---

diff --git a/src/allmydata/test/test_cli_magic_folder.py b/src/allmydata/test/test_cli_magic_folder.py
index 33b8c96b..abb2344d 100644
--- a/src/allmydata/test/test_cli_magic_folder.py
+++ b/src/allmydata/test/test_cli_magic_folder.py
@@ -11,6 +11,7 @@ from .test_cli import CLITestMixin
 from allmydata.scripts import magic_folder_cli
 from allmydata.util.fileutil import abspath_expanduser_unicode
 from allmydata.frontends.magic_folder import MagicFolder
+from allmydata.frontends.magic_folder import Downloader
 from allmydata import uri
 
 
@@ -202,3 +203,34 @@ class CreateMagicFolder(MagicFolderTestMixin, unittest.TestCase):
         d.addCallback(lambda x: self.check_config(0, self.local_dir))
         return d
 
+    def test_write_downloaded_file(self):
+        workdir = u"cli/MagicFolder/write-downloaded-file"
+        local_file = fileutil.abspath_expanduser_unicode(os.path.join(workdir, "foobar"))
+
+        # create a file with name "foobar" with content "foo"
+        # write downloaded file content "bar" into "foobar" with is_conflict = False
+        fileutil.make_dirs(workdir)
+        fileutil.write(local_file, "foo")
+
+        # if is_conflict is False, then the .conflict file shouldn't exist.
+        Downloader._write_downloaded_file(local_file, "bar", False, None)
+        conflicted_path = local_file + u".conflict"
+        self.failIf(os.path.exists(conflicted_path))
+
+        # At this point, the backup file should exist with content "foo"
+        backup_path = local_file + u".backup"
+        self.failUnless(os.path.exists(backup_path))
+        self.failUnlessEqual(fileutil.read(backup_path), "foo")
+
+        # .tmp file shouldn't exist
+        self.failIf(os.path.exists(local_file + u".tmp"))
+
+        # .. and the original file should have the new content
+        self.failUnlessEqual(fileutil.read(local_file), "bar")
+
+        # now a test for conflicted case
+        Downloader._write_downloaded_file(local_file, "bar", True, None)
+        self.failUnless(os.path.exists(conflicted_path))
+
+        # .tmp file shouldn't exist
+        self.failIf(os.path.exists(local_file + u".tmp"))