def test_doccommander_add__while_running(): dc = rc.DocCommander() dc.add('sleep 2\n', ['bash', 'test1']) t = threading.Thread(target=doccommander_worker, args=(dc, )) t.start() time.sleep(1) with pytest.raises(RundocException): dc.add('echo "bad"\n', ['bash', 'test1'])
def test_parsers__parse_doc__single_session(): f = io.StringIO() data = '```env\na=b\n```\n```bash#test1\nls\n```\n\n```bash#test2\nls -al\n```' expected = rc.DocCommander() expected.add('ls\nls -al\n', ['bash']) expected.env.import_string("a=b") f.write(data) f.seek(0) c = rp.parse_doc(f, single_session='bash') assert c.get_dict() == expected.get_dict()
def test_parsers__parse_doc(): f = io.StringIO() data = '```env\na=b\n```\n```bash#test1\nls\n```\n\n```bash#test2\nls -al\n```' expected = rc.DocCommander() expected.add('ls\n', ['bash', 'test1']) expected.add('ls -al\n', ['bash', 'test2']) f.write(data) f.seek(0) c = rp.parse_doc(f, 'bash') assert c.get_dict() == expected.get_dict()
def test_doccommander_run(dummy_file): dc = rc.DocCommander() dc.add('echo "test1"\n', ['bash', 'test1']) dc.add('echo "test2"\n', ['bash', 'test1']) dc.add('echo "test3"\n', ['bash', 'test1']) with open(dummy_file, 'w') as f: dc.run(inherit_env=True, output=f) assert len(dc.get_dict()['code_blocks']) == 3 for cb in dc.get_dict()['code_blocks']: assert len(cb['runs']) == 1
def test_doccommander_write_output(dummy_file): dc = rc.DocCommander() dc.add('echo "test"\n', ['bash', 'test1']) dc.run() with open(dummy_file, 'w') as f: dc.output = f dc.write_output() with open(dummy_file, 'r') as f: output = json.loads(f.read()) assert len(output['code_blocks']) == 1
def test_doccommander_get_dict(): dc = rc.DocCommander() dc.add('ls\n', ['bash', 'test1']) assert dc.get_dict() == { "code_blocks": [{ "code": "ls\n", "tags": ["bash", "test1"], "interpreter": "bash", "runs": [] }], "env": {} }
def test_doccommander_die_with_grace(dummy_file): dc = rc.DocCommander() dc.add('echo "test"\n', ['bash', 'test1']) dc.add('sleep 2\n', ['bash', 'test1']) with open(dummy_file, 'w') as f: dc.output = f t = threading.Thread(target=doccommander_worker, args=(dc, )) t.start() time.sleep(1) dc.die_with_grace() with open(dummy_file, 'r') as f: output = json.loads(f.read()) assert output['code_blocks'][0]['runs'][0]['output'] == 'test\n'
def test_doccommander_add(): dc = rc.DocCommander() assert len(dc.doc_blocks) == 0 dc.add('ls\n', ['bash', 'test1']) assert len(dc.doc_blocks) == 1 assert dc.doc_blocks[0].code == 'ls\n' assert dc.doc_blocks[0].tags == ['bash', 'test1'] dc.add('ls -al\n', ['bash', 'test2']) assert len(dc.doc_blocks) == 2 assert dc.doc_blocks[0].code == 'ls\n' assert dc.doc_blocks[0].tags == ['bash', 'test1'] assert dc.doc_blocks[1].code == 'ls -al\n' assert dc.doc_blocks[1].tags == ['bash', 'test2']
def test_doccommander_run__failed(): dc = rc.DocCommander() dc.add('cat /non_existent', ['bash', 'test1']) with pytest.raises(CodeFailed): dc.run(retry=5, retry_pause=0.1)
def test_doccommander_add__unknown_interpreter(): dc = rc.DocCommander() with pytest.raises(SystemExit): dc.add('sleep 1\n', ['unknown', 'test1'])
def test_doccommander_doc_block__no_step(): dc = rc.DocCommander() dc.add('ls\n', ['bash', 'test1']) assert dc.doc_block == None
def test_doccommander_doc_block__step(): dc = rc.DocCommander() dc.add('ls\n', ['bash', 'test1']) dc.step = 1 assert dc.doc_block == dc.doc_blocks[0]