def test_import_does_not_overwrite(self): with FakeFileOpen(self.fs)('data/real.bib', 'w') as f: f.write(str_fixtures.bibtex_external0) with FakeFileOpen(self.fs)('data/fake.bib', 'w') as f: f.write(str_fixtures.bibtex_external_alt) cmds = ['pubs init', 'pubs import data/real.bib Page99', 'pubs list', 'pubs import data/fake.bib Page99', 'pubs list', ] outs = self.execute_cmds(cmds) self.assertEqual(outs[-3], outs[-1])
def read_text(self, encoding=None, errors=None): """ Open the fake file in text mode, read it, and close the file. """ with FakeFileOpen(self.filesystem)( self._path(), mode='r', encoding=encoding, errors=errors) as f: return f.read()
def write_text(self, data, encoding=None, errors=None, newline=None): """Open the fake file in text mode, write to it, and close the file. Args: data: the string to be written encoding: the encoding used for the string; if not given, the default locale encoding is used errors: (str) Defines how encoding errors are handled. newline: Controls universal newlines, passed to stream object. New in Python 3.10. Raises: TypeError: if data is not of type 'str'. OSError: if the target object is a directory, the path is invalid or permission is denied. """ if not isinstance(data, str): raise TypeError('data must be str, not %s' % data.__class__.__name__) if newline is not None and sys.version_info < (3, 10): raise TypeError("write_text() got an unexpected " "keyword argument 'newline'") with FakeFileOpen(self.filesystem)(self._path(), mode='w', encoding=encoding, errors=errors, newline=newline) as f: return f.write(data)
def test_import_fails_without_ignore(self): with FakeFileOpen(self.fs)('data/fake.bib', 'w') as f: f.write(str_fixtures.not_bibtex) cmds = ['pubs init', 'pubs import data/ Page99', ] with self.assertRaises(FakeSystemExit): self.execute_cmds(cmds)
def read_bytes(self): """Open the fake file in bytes mode, read it, and close the file. Raises: IOError: if the target object is a directory, the path is invalid or permission is denied. """ with FakeFileOpen(self.filesystem)(self._path(), mode='rb') as f: return f.read()
def test_import_ignores(self): with FakeFileOpen(self.fs)('data/fake.bib', 'w') as f: f.write(str_fixtures.not_bibtex) cmds = ['pubs init', 'pubs import --ignore-malformed data/ Page99', 'pubs list' ] outs = self.execute_cmds(cmds) self.assertEqual(1 + 1, len(outs[-1].split('\n')))
def write_bytes(self, data): """Open the fake file in bytes mode, write to it, and close the file. Args: data: the bytes to be written Raises: IOError: if the target object is a directory, the path is invalid or permission is denied. """ # type-check for the buffer interface before truncating the file view = memoryview(data) with FakeFileOpen(self.filesystem)(self._path(), mode='wb') as f: return f.write(view)
def open(self, mode='r', buffering=-1, encoding=None, errors=None, newline=None): """Open the file pointed by this path and return a fake file object. Raises: IOError: if the target object is a directory, the path is invalid or permission is denied. """ if self._closed: self._raise_closed() return FakeFileOpen(self.filesystem)( self._path(), mode, buffering, encoding, errors, newline)
def write_text(self, data, encoding=None, errors=None): """Open the fake file in text mode, write to it, and close the file. Args: data: the string to be written encoding: the encoding used for the string; if not given, the default locale encoding is used errors: ignored Raises: TypeError: if data is not of type 'str' IOError: if the target object is a directory, the path is invalid or permission is denied. """ if not isinstance(data, str): raise TypeError('data must be str, not %s' % data.__class__.__name__) with FakeFileOpen(self.filesystem)( self._path(), mode='w', encoding=encoding, errors=errors) as f: return f.write(data)
def url_to_byte_content(self, url, ui=None): path = self._url_to_path(url) with FakeFileOpen(self.fs)('data' + path, 'rb') as f: return f.read()