def test_save(mocker, filename='nonfilename', directory='nondirectory'): source = Source(**SOURCE) makedirs = mocker.patch('os.makedirs', autospec=True) open_ = mocker.patch('builtins.open', mocker.mock_open()) assert source.save(filename, directory) == source.filepath assert source.filename == filename and source.directory == directory makedirs.assert_called_once_with(source.directory, 0o777, exist_ok=True) open_.assert_called_once_with(source.filepath, 'w', encoding=source.encoding) assert open_.return_value.write.call_args_list == [mocker.call(source.source), mocker.call('\n')]
def test_save(mocker, py2, filename='filename', directory='directory'): source = Source(**SOURCE) makedirs = mocker.patch('os.makedirs') open_ = mocker.patch('io.open', mocker.mock_open()) assert source.save(filename, directory) == source.filepath assert source.filename == filename and source.directory == directory if py2: makedirs.assert_called_once_with(source.directory, 0o777) else: makedirs.assert_called_once_with(source.directory, 0o777, exist_ok=True) open_.assert_called_once_with(source.filepath, 'w', encoding=source.encoding) assert open_.return_value.write.call_args_list == [((source.source,),), ((u'\n',),)]
def test_from_file(tmpdir, filename='hello.gv', directory='source_hello', data=u'digraph { hello -> world }', encoding='utf-8'): lpath = tmpdir.mkdir(directory) lpath.join(filename).write_text(data, encoding=encoding) source = Source.from_file(filename, str(lpath)) assert source.encoding == 'utf-8' source = Source.from_file(filename, str(lpath), encoding=encoding) assert source.source == data assert source.filename == filename assert source.directory == str(lpath) assert source.encoding == encoding
def test_save(mocker, py2, filename='filename', directory='directory'): source = Source(**SOURCE) makedirs = mocker.patch('os.makedirs', new_callable=mocker.Mock) open_ = mocker.patch('io.open', mocker.mock_open()) assert source.save(filename, directory) == source.filepath assert source.filename == filename and source.directory == directory if py2: makedirs.assert_called_once_with(source.directory, 0o777) else: makedirs.assert_called_once_with(source.directory, 0o777, exist_ok=True) open_.assert_called_once_with(source.filepath, 'w', encoding=source.encoding) assert open_.return_value.write.call_args_list == [((source.source,),), ((u'\n',),)]
def test_from_file(tmpdir, filename='hello.gv', directory='source_hello', data=u'digraph { hello -> world }', encoding='utf-8'): lpath = tmpdir.mkdir(directory) (lpath / filename).write_text(data, encoding=encoding) source = Source.from_file(filename, str(lpath)) assert source.encoding == 'utf-8' source = Source.from_file(filename, str(lpath), encoding=None) assert source.encoding == locale.getpreferredencoding() source = Source.from_file(filename, str(lpath), encoding=encoding) assert source.source == data assert source.filename == filename assert source.directory == str(lpath) assert source.encoding == encoding
def test_from_file(tmp_path, filename='hello.gv', directory='source_hello', data='digraph { hello -> world }', encoding='utf-8'): lpath = tmp_path / directory lpath.mkdir() (lpath / filename).write_text(data, encoding=encoding) source = Source.from_file(filename, str(lpath)) assert source.encoding == 'utf-8' source = Source.from_file(filename, str(lpath), encoding=None) assert source.encoding == locale.getpreferredencoding() source = Source.from_file(filename, str(lpath), encoding=encoding) assert source.source == data assert source.filename == filename assert source.directory == str(lpath) assert source.encoding == encoding
def get_graphviz_source(filename, directory=None, format='dot', engine='dot'): from graphviz.files import Source src = Source.from_file(filename, directory=directory, format=format, engine=engine) src.filename = filename[:-4] return src
def test_init_filename(): assert Source('').filename == 'Source.gv' assert type('Named', (Source,), {'name': 'name'})('').filename == 'name.gv'
def source(): return Source(**SOURCE)
def test_init(self): source = 'graph { hello -> world }' s = Source(source) self.assertEqual(s.source, source)