Beispiel #1
0
    def test_generated_file_function(self):
        """
        Test GeneratedFile behavior with functions.
        """
        dest = self.tmppath("dest")
        data = {
            "num_calls": 0,
        }

        def content():
            data["num_calls"] += 1
            return b"content"

        f = GeneratedFile(content)
        self.assertEqual(data["num_calls"], 0)
        f.copy(dest)
        self.assertEqual(data["num_calls"], 1)
        self.assertEqual(b"content", open(dest, "rb").read())
        self.assertEqual(b"content", f.open().read())
        self.assertEqual(b"content", f.read())
        self.assertEqual(len(b"content"), f.size())
        self.assertEqual(data["num_calls"], 1)

        f.content = b"modified"
        f.copy(dest)
        self.assertEqual(data["num_calls"], 1)
        self.assertEqual(b"modified", open(dest, "rb").read())
        self.assertEqual(b"modified", f.open().read())
        self.assertEqual(b"modified", f.read())
        self.assertEqual(len(b"modified"), f.size())

        f.content = content
        self.assertEqual(data["num_calls"], 1)
        self.assertEqual(b"content", f.read())
        self.assertEqual(data["num_calls"], 2)
Beispiel #2
0
    def test_generated_file_function(self):
        '''
        Test GeneratedFile behavior with functions.
        '''
        dest = self.tmppath('dest')
        data = {
            'num_calls': 0,
        }

        def content():
            data['num_calls'] += 1
            return b'content'

        f = GeneratedFile(content)
        self.assertEqual(data['num_calls'], 0)
        f.copy(dest)
        self.assertEqual(data['num_calls'], 1)
        self.assertEqual(b'content', open(dest, 'rb').read())
        self.assertEqual(b'content', f.open().read())
        self.assertEqual(b'content', f.read())
        self.assertEqual(len(b'content'), f.size())
        self.assertEqual(data['num_calls'], 1)

        f.content = b'modified'
        f.copy(dest)
        self.assertEqual(data['num_calls'], 1)
        self.assertEqual(b'modified', open(dest, 'rb').read())
        self.assertEqual(b'modified', f.open().read())
        self.assertEqual(b'modified', f.read())
        self.assertEqual(len(b'modified'), f.size())

        f.content = content
        self.assertEqual(data['num_calls'], 1)
        self.assertEqual(b'content', f.read())
        self.assertEqual(data['num_calls'], 2)
Beispiel #3
0
    def test_generated_file(self):
        """
        Check that GeneratedFile.copy yields the proper content in the
        destination file in all situations that trigger different code paths
        (see TestFile.test_file)
        """
        dest = self.tmppath("dest")

        for content in samples:
            f = GeneratedFile(content)
            f.copy(dest)
            self.assertEqual(content, open(dest, "rb").read())
Beispiel #4
0
    def test_generated_file(self):
        '''
        Check that GeneratedFile.copy yields the proper content in the
        destination file in all situations that trigger different code paths
        (see TestFile.test_file)
        '''
        dest = self.tmppath('dest')

        for content in samples:
            f = GeneratedFile(content)
            f.copy(dest)
            self.assertEqual(content, open(dest, 'rb').read())
Beispiel #5
0
    def test_generated_file_no_write(self):
        """
        Test various conditions where GeneratedFile.copy is expected not to
        write in the destination file.
        """
        dest = self.tmppath("dest")

        # Initial copy
        f = GeneratedFile(b"test")
        f.copy(dest)

        # Ensure subsequent copies won't trigger writes
        f.copy(DestNoWrite(dest))
        self.assertEqual(b"test", open(dest, "rb").read())

        # When using a new instance with the same content, no copy should occur
        f = GeneratedFile(b"test")
        f.copy(DestNoWrite(dest))
        self.assertEqual(b"test", open(dest, "rb").read())

        # Double check that under conditions where a copy occurs, we would get
        # an exception.
        f = GeneratedFile(b"fooo")
        self.assertRaises(RuntimeError, f.copy, DestNoWrite(dest))
Beispiel #6
0
    def test_generated_file_no_write(self):
        '''
        Test various conditions where GeneratedFile.copy is expected not to
        write in the destination file.
        '''
        dest = self.tmppath('dest')

        # Initial copy
        f = GeneratedFile('test')
        f.copy(dest)

        # Ensure subsequent copies won't trigger writes
        f.copy(DestNoWrite(dest))
        self.assertEqual('test', open(dest, 'rb').read())

        # When using a new instance with the same content, no copy should occur
        f = GeneratedFile('test')
        f.copy(DestNoWrite(dest))
        self.assertEqual('test', open(dest, 'rb').read())

        # Double check that under conditions where a copy occurs, we would get
        # an exception.
        f = GeneratedFile('fooo')
        self.assertRaises(RuntimeError, f.copy, DestNoWrite(dest))