Merge pull request #1574 from coreos-inc/generator-buffered

Fix GeneratorFile for working with BufferedReader
This commit is contained in:
josephschorr 2016-06-23 16:35:39 -04:00 committed by GitHub
commit 8232a14518
2 changed files with 31 additions and 0 deletions

View file

@ -1,4 +1,5 @@
import unittest import unittest
import magic
from itertools import islice from itertools import islice
from semantic_version import Version, Spec from semantic_version import Version, Spec
@ -7,6 +8,7 @@ from util.validation import generate_valid_usernames
from util.registry.generatorfile import GeneratorFile from util.registry.generatorfile import GeneratorFile
from util.registry.dockerver import docker_version from util.registry.dockerver import docker_version
from util import slash_join from util import slash_join
from _pyio import BufferedReader
class TestGeneratorFile(unittest.TestCase): class TestGeneratorFile(unittest.TestCase):
def sample_generator(self): def sample_generator(self):
@ -76,6 +78,32 @@ class TestGeneratorFile(unittest.TestCase):
self.assertEquals("thisisatest", f.read(60)) self.assertEquals("thisisatest", f.read(60))
self.assertEquals(11, f.tell()) self.assertEquals(11, f.tell())
def test_with_bufferedreader(self):
with GeneratorFile(self.sample_generator()) as f:
buffered = BufferedReader(f)
self.assertEquals("thisisatest", buffered.peek(10))
self.assertEquals("thisisates", buffered.read(10))
def mimed_html_generator(self):
yield '<html>'
yield '<body>'
yield 'sometext' * 1024
yield '</body>'
yield '</html>'
def test_magic(self):
mgc = magic.Magic(mime=True)
with GeneratorFile(self.mimed_html_generator()) as f:
buffered = BufferedReader(f)
file_header_bytes = buffered.peek(1024)
self.assertEquals("text/html", mgc.from_buffer(file_header_bytes))
with GeneratorFile(self.sample_generator()) as f:
buffered = BufferedReader(f)
file_header_bytes = buffered.peek(1024)
self.assertEquals("text/plain", mgc.from_buffer(file_header_bytes))
class TestUsernameGenerator(unittest.TestCase): class TestUsernameGenerator(unittest.TestCase):
def assert_generated_output(self, input_username, expected_output): def assert_generated_output(self, input_username, expected_output):

View file

@ -33,6 +33,9 @@ class GeneratorFile(object):
raise StopIteration raise StopIteration
return r return r
def readable(self):
return not self._closed
def readline(self): def readline(self):
buf = [] buf = []
while True: while True: