Exemplo n.º 1
0
class TestProfileCache(FileSystemTestCase):
    def setUp(self):
        FileSystemTestCase.setUp(self)
        self.diskIo = DiskIo()
        self.settings = Settings(self.getWorkingDir())
        self.diskIo.createDirWithParents(self.settings.cachePath)
        self.profileCache = ProfileCache(self.settings, self.diskIo)
        self.testProfile = Profile('test/vimrc')

    # ProfileCache.contains

    def test_contains_whenDirExists_returnsTrue(self):
        profileDir = self.getTestPath('.vimswitch/profiles/test.vimrc')
        self.diskIo.createDir(profileDir)

        result = self.profileCache.contains(self.testProfile)

        self.assertTrue(result)

    def test_contains_whenDirDoesNotExist_returnsFalse(self):
        result = self.profileCache.contains(self.testProfile)
        self.assertFalse(result)

    # ProfileCache.delete

    def test_delete_deletesProfile(self):
        profileDir = self.getTestPath('.vimswitch/profiles/test.vimrc')
        vimrcFilePath = self.getTestPath(
            '.vimswitch/profiles/test.vimrc/.vimrc')
        self.diskIo.createDir(profileDir)
        self.diskIo.createFile(vimrcFilePath, 'test data')

        self.profileCache.delete(self.testProfile)

        self.assertFalse(self.profileCache.contains(self.testProfile))

    # ProfileCache.getProfileLocation

    def test_getProfileLocation(self):
        self.settings.cachePath = '/foo/bar/cachePath'
        profile = Profile('test/vimrc')
        result = self.profileCache.getProfileLocation(profile)
        self.assertEquals(result,
                          os.path.normpath('/foo/bar/cachePath/test.vimrc'))

    # ProfileCache.createEmptyProfile

    def test_createEmptyProfile_profileDoesNotExist_createsProfileDir(self):
        profile = Profile('default')

        self.profileCache.createEmptyProfile(profile)

        profileDir = self.getTestPath('.vimswitch/profiles/default')
        self.assertTrue(self.diskIo.dirExists(profileDir))
Exemplo n.º 2
0
class TestProfileCache(FileSystemTestCase):

    def setUp(self):
        FileSystemTestCase.setUp(self)
        self.diskIo = DiskIo()
        self.settings = Settings(self.getWorkingDir())
        self.diskIo.createDirWithParents(self.settings.cachePath)
        self.profileCache = ProfileCache(self.settings, self.diskIo)
        self.testProfile = Profile('test/vimrc')

    # ProfileCache.contains

    def test_contains_whenDirExists_returnsTrue(self):
        profileDir = self.getTestPath('.vimswitch/profiles/test.vimrc')
        self.diskIo.createDir(profileDir)

        result = self.profileCache.contains(self.testProfile)

        self.assertTrue(result)

    def test_contains_whenDirDoesNotExist_returnsFalse(self):
        result = self.profileCache.contains(self.testProfile)
        self.assertFalse(result)

    # ProfileCache.delete

    def test_delete_deletesProfile(self):
        profileDir = self.getTestPath('.vimswitch/profiles/test.vimrc')
        vimrcFilePath = self.getTestPath('.vimswitch/profiles/test.vimrc/.vimrc')
        self.diskIo.createDir(profileDir)
        self.diskIo.createFile(vimrcFilePath, 'test data')

        self.profileCache.delete(self.testProfile)

        self.assertFalse(self.profileCache.contains(self.testProfile))

    # ProfileCache.getProfileLocation

    def test_getProfileLocation(self):
        self.settings.cachePath = '/foo/bar/cachePath'
        profile = Profile('test/vimrc')
        result = self.profileCache.getProfileLocation(profile)
        self.assertEquals(result, os.path.normpath('/foo/bar/cachePath/test.vimrc'))

    # ProfileCache.createEmptyProfile

    def test_createEmptyProfile_profileDoesNotExist_createsProfileDir(self):
        profile = Profile('default')

        self.profileCache.createEmptyProfile(profile)

        profileDir = self.getTestPath('.vimswitch/profiles/default')
        self.assertTrue(self.diskIo.dirExists(profileDir))
Exemplo n.º 3
0
class TestFileDownloader(FileSystemTestCase):
    @classmethod
    def setUpClass(cls):
        cls.host = 'localhost'
        cls.server = SimpleServer(cls.getDataPath(''), cls.host)
        cls.server.start()
        cls.port = cls.server.getPort()

    @classmethod
    def tearDownClass(cls):
        cls.server.stop()

    def setUp(self):
        FileSystemTestCase.setUp(self)
        settings = Settings()
        settings.downloadsPath = self.getTestPath('')
        self.diskIo = DiskIo()
        self.fileDownloader = FileDownloader(settings, self.diskIo)

    # FileDownloader.download

    def test_download_downloadsFile(self):
        url = self.getLocalUrl('simple.txt')
        downloadPath = self.getTestPath('simple_downloaded.txt')

        self.fileDownloader.download(url, downloadPath)

        actual = self.diskIo.getFileContents(downloadPath)
        expected = 'test data'
        self.assertEqual(actual, expected)

    @attr('external')
    def test_download_downloadsFromHttps(self):
        url = 'https://github.com/priomsrb/vimrc/archive/master.zip'
        downloadDir = self.getTestPath('')

        downloadPath = self.fileDownloader.download(url, downloadDir)

        self.assertTrue(self.diskIo.fileExists(downloadPath))

    def test_download_fileAlreadyExists_overwritesFile(self):
        url = self.getLocalUrl('simple.txt')
        downloadPath = self.getTestPath('simple_downloaded.txt')
        self.diskIo.createFile(downloadPath, 'previous data')

        self.fileDownloader.download(url, downloadPath)

        actual = self.diskIo.getFileContents(downloadPath)
        expected = 'test data'
        self.assertEqual(actual, expected)

    def test_download_nonExistantUrl_raisesError(self):
        nonExistantUrl = self.getLocalUrl('non_existant.txt')
        downloadDir = self.getTestPath('')

        self.assertRaises(IOError, self.fileDownloader.download, nonExistantUrl, downloadDir)

    def test_download_dirAsDestination_keepsOriginalFilename(self):
        url = self.getLocalUrl('simple.txt')
        downloadDir = self.getTestPath('')

        downloadPath = self.fileDownloader.download(url, downloadDir)

        self.assertEqual(downloadPath, self.getTestPath('simple.txt'))
        actual = self.diskIo.getFileContents(downloadPath)
        expected = 'test data'
        self.assertEqual(actual, expected)

    def test_download_withMissingFilename_generatesFilename(self):
        url = self.getLocalUrl('simple.txt')
        downloadDir = self.getTestPath('')
        self.fileDownloader._getDownloadFilename = MagicMock(FileDownloader._getDownloadFilename)
        self.fileDownloader._getDownloadFilename.return_value = ''

        downloadPath = self.fileDownloader.download(url, downloadDir)

        self.assertTrue(len(downloadPath) > 0)
        actual = self.diskIo.getFileContents(downloadPath)
        expected = 'test data'
        self.assertEqual(actual, expected)

    # FileDownloader._getDownloadFilename

    def test_getDownloadFilename_parsesUrl(self):
        url = 'http://example.com/foo/bar/url_filename.txt?q=1'
        headers = Message()

        filename = self.fileDownloader._getDownloadFilename(url, headers)

        self.assertEqual(filename, 'url_filename.txt')

    def test_getDownloadFilename_parsesHeaderFirst(self):
        url = 'http://example.com/foo/bar/url_filename.txt?q=1'
        headers = Message()
        headers['Server'] = 'SimpleHTTP/0.6 Python/2.7.5'
        headers['Date'] = 'Tue, 09 Sep 2014 02:51:53 GMT'
        headers['Content-type'] = 'text/plain'
        headers['Content-Length'] = '9'
        headers['Last-Modified'] = 'Mon, 08 Sep 2014 23:53:51 GMT'
        # content-disposition should specify the filename
        headers['content-disposition'] = 'attachment; filename=header_filename.txt'

        filename = self.fileDownloader._getDownloadFilename(url, headers)

        self.assertEqual(filename, 'header_filename.txt')

    def test_getDownloadFilename_invalidHeader_usesUrl(self):
        url = 'http://example.com/foo/bar/url_filename.txt?q=1'
        headers = Message()
        headers['Server'] = 'SimpleHTTP/0.6 Python/2.7.5'
        headers['Date'] = 'Tue, 09 Sep 2014 02:51:53 GMT'
        headers['Content-type'] = 'text/plain'
        headers['Content-Length'] = '9'
        headers['Last-Modified'] = 'Mon, 08 Sep 2014 23:53:51 GMT'
        # content-disposition should specify the filename
        headers['content-disposition'] = 'malformed_section'

        filename = self.fileDownloader._getDownloadFilename(url, headers)

        self.assertEqual(filename, 'url_filename.txt')

    # Helpers

    def getLocalUrl(self, path):
        return 'http://' + self.host + ':' + str(self.port) + '/' + path
Exemplo n.º 4
0
class TestFileDownloader(FileSystemTestCase):
    @classmethod
    def setUpClass(cls):
        cls.host = 'localhost'
        cls.server = SimpleServer(cls.getDataPath(''), cls.host)
        cls.server.start()
        cls.port = cls.server.getPort()

    @classmethod
    def tearDownClass(cls):
        cls.server.stop()

    def setUp(self):
        FileSystemTestCase.setUp(self)
        settings = Settings()
        settings.downloadsPath = self.getTestPath('')
        self.diskIo = DiskIo()
        self.fileDownloader = FileDownloader(settings, self.diskIo)

    # FileDownloader.download

    def test_download_downloadsFile(self):
        url = self.getLocalUrl('simple.txt')
        downloadPath = self.getTestPath('simple_downloaded.txt')

        self.fileDownloader.download(url, downloadPath)

        actual = self.diskIo.getFileContents(downloadPath)
        expected = 'test data'
        self.assertEqual(actual, expected)

    @attr('external')
    def test_download_downloadsFromHttps(self):
        url = 'https://github.com/priomsrb/vimrc/archive/master.zip'
        downloadDir = self.getTestPath('')

        downloadPath = self.fileDownloader.download(url, downloadDir)

        self.assertTrue(self.diskIo.fileExists(downloadPath))

    def test_download_fileAlreadyExists_overwritesFile(self):
        url = self.getLocalUrl('simple.txt')
        downloadPath = self.getTestPath('simple_downloaded.txt')
        self.diskIo.createFile(downloadPath, 'previous data')

        self.fileDownloader.download(url, downloadPath)

        actual = self.diskIo.getFileContents(downloadPath)
        expected = 'test data'
        self.assertEqual(actual, expected)

    def test_download_nonExistantUrl_raisesError(self):
        nonExistantUrl = self.getLocalUrl('non_existant.txt')
        downloadDir = self.getTestPath('')

        self.assertRaises(IOError, self.fileDownloader.download,
                          nonExistantUrl, downloadDir)

    def test_download_dirAsDestination_keepsOriginalFilename(self):
        url = self.getLocalUrl('simple.txt')
        downloadDir = self.getTestPath('')

        downloadPath = self.fileDownloader.download(url, downloadDir)

        self.assertEqual(downloadPath, self.getTestPath('simple.txt'))
        actual = self.diskIo.getFileContents(downloadPath)
        expected = 'test data'
        self.assertEqual(actual, expected)

    def test_download_withMissingFilename_generatesFilename(self):
        url = self.getLocalUrl('simple.txt')
        downloadDir = self.getTestPath('')
        self.fileDownloader._getDownloadFilename = MagicMock(
            FileDownloader._getDownloadFilename)
        self.fileDownloader._getDownloadFilename.return_value = ''

        downloadPath = self.fileDownloader.download(url, downloadDir)

        self.assertTrue(len(downloadPath) > 0)
        actual = self.diskIo.getFileContents(downloadPath)
        expected = 'test data'
        self.assertEqual(actual, expected)

    # FileDownloader._getDownloadFilename

    def test_getDownloadFilename_parsesUrl(self):
        url = 'http://example.com/foo/bar/url_filename.txt?q=1'
        headers = Message()

        filename = self.fileDownloader._getDownloadFilename(url, headers)

        self.assertEqual(filename, 'url_filename.txt')

    def test_getDownloadFilename_parsesHeaderFirst(self):
        url = 'http://example.com/foo/bar/url_filename.txt?q=1'
        headers = Message()
        headers['Server'] = 'SimpleHTTP/0.6 Python/2.7.5'
        headers['Date'] = 'Tue, 09 Sep 2014 02:51:53 GMT'
        headers['Content-type'] = 'text/plain'
        headers['Content-Length'] = '9'
        headers['Last-Modified'] = 'Mon, 08 Sep 2014 23:53:51 GMT'
        # content-disposition should specify the filename
        headers[
            'content-disposition'] = 'attachment; filename=header_filename.txt'

        filename = self.fileDownloader._getDownloadFilename(url, headers)

        self.assertEqual(filename, 'header_filename.txt')

    def test_getDownloadFilename_invalidHeader_usesUrl(self):
        url = 'http://example.com/foo/bar/url_filename.txt?q=1'
        headers = Message()
        headers['Server'] = 'SimpleHTTP/0.6 Python/2.7.5'
        headers['Date'] = 'Tue, 09 Sep 2014 02:51:53 GMT'
        headers['Content-type'] = 'text/plain'
        headers['Content-Length'] = '9'
        headers['Last-Modified'] = 'Mon, 08 Sep 2014 23:53:51 GMT'
        # content-disposition should specify the filename
        headers['content-disposition'] = 'malformed_section'

        filename = self.fileDownloader._getDownloadFilename(url, headers)

        self.assertEqual(filename, 'url_filename.txt')

    # Helpers

    def getLocalUrl(self, path):
        return 'http://' + self.host + ':' + str(self.port) + '/' + path