def test_touch_updates_mtime_leaves_content_intact(tmpdir): reference = str(tmpdir / 'reference') with open(reference, 'w') as r: r.write('Hello world') c = Component() os.utime(reference, (0, 0)) c.touch(reference) assert os.stat(reference).st_mtime != 0 with open(reference, 'r') as r: assert 'Hello world' == r.read()
def test_touch_updates_mtime_leaves_content_intact(tmpdir): reference = str(tmpdir / "reference") with open(reference, "w") as r: r.write("Hello world") c = Component() os.utime(reference, (0, 0)) c.touch(reference) assert os.stat(reference).st_mtime != 0 with open(reference, "r") as r: assert "Hello world" == r.read()
def test_chdir_contextmanager_is_stackable(): outer = os.getcwd() inner1 = os.path.join(os.path.dirname(__file__), 'fixture') inner2 = os.path.join(os.path.dirname(__file__)) c = Component() with c.chdir(inner1): assert inner1 == os.getcwd() with c.chdir(inner2): assert inner2 == os.getcwd() # noqa assert inner1 == os.getcwd() assert outer == os.getcwd()
def test_component_with_unknown_arguments_raises_valueerror(): # Regression against bug #65 with pytest.raises(ValueError) as e: Component("foo") assert str(e.value) == "Namevar is undefined for this component." with pytest.raises(ValueError) as e: Component(asdf="foo") assert str(e.value) == ( "The following arguments are unacceptable for this component " "because they are either undefined or would override " "methods: asdf")
def test_recursive_sub_component_iterator(root): for x in range(3): c = Component(name='x{}'.format(x)) root.component += c for y in range(2): c2 = Component(name='x{}y{}'.format(x, y)) c += c2 recursive = list(x.name for x in root.component.recursive_sub_components) assert (['x0', 'x0y0', 'x0y1', 'x1', 'x1y0', 'x1y1', 'x2', 'x2y0', 'x2y1'] == recursive)
def test_acic_does_not_raise_if_newer(root): c = Component() c.last_updated = Mock(return_value=22) root.component += c c2 = Component() c2.last_updated = Mock(return_value=21) c.assert_component_is_current(c2)
def test_acic_accepts_multiple_components(root): c = Component() c.last_updated = Mock(return_value=20) root.component += c c2 = Component() c2.last_updated = Mock(return_value=21) with pytest.raises(batou.UpdateNeeded): c.assert_component_is_current([c2, c2])
def test_acic_raises_if_older_reference(root): c = Component() c.last_updated = Mock(return_value=20) root.component += c c2 = Component() c2.last_updated = Mock(return_value=21) with pytest.raises(batou.UpdateNeeded): c.assert_component_is_current(c2)
def test_cmd_execution_failed_gives_command_in_exception(mockroot): c = Component() c.prepare(mockroot) with pytest.raises(CmdExecutionError) as e: c.cmd('asdf') assert e.value.cmd == 'asdf' assert e.value.returncode == 127
def test_cmd_should_not_stop_if_process_expects_input(mockroot): c = Component() c.prepare(mockroot) stdout, stderr = c.cmd('cat')
def test_init_with_no_arguments_creates_plain_component(): component = Component() # Lazy initialized attribute assert not hasattr(component, 'sub_components')
def test_init_keyword_args_update_dict(): component = Component(foobar=1) assert 1 == component.foobar
def test_assert_no_changes_recursive_does_not_raise(root): root.component += Component() root.component.assert_no_changes()
def test_assert_cmd_when_succesful(mockroot): c = Component() c.prepare(mockroot) c.assert_cmd('true')
def test_require_one_convenience_raises_if_no_result(): c = Component() c.require = Mock(return_value=[]) with pytest.raises(SilentConfigurationError): c.require_one('asdf')
def test_plain_component_runs_noop_configure_verify_update(): component = Component() component.configure() component.verify() component.update()
def test_cmd_returns_output(mockroot): c = Component() c.prepare(mockroot) assert ('1\n', '') == c.cmd('echo 1')
def test_cmd_expands_jinja(mockroot): c = Component() c.foo = 'asdf' c.prepare(mockroot) assert ('asdf\n', '') == c.cmd('echo "{{component.foo}}"')
def test_last_updated_not_implemented_on_base(): c = Component() with pytest.raises(NotImplementedError): c.last_updated()
def test_cmd_raises_if_error(mockroot): c = Component() c.prepare(mockroot) with pytest.raises(CmdExecutionError): c.cmd('non-existing-command')
def test_require_one_convenience_api_returns_scalar(): c = Component() c.require = Mock(return_value=[1]) assert 1 == c.require_one('asdf')
def test_cmd_returns_output_if_ignore_returncode(mockroot): c = Component() c.prepare(mockroot) out, err = c.cmd('echo important output && false', ignore_returncode=True) assert 'important output\n' == out
def test_require_one_convenience_raises_if_more_results(): c = Component() c.require = Mock(return_value=[1, 2]) with pytest.raises(KeyError): c.require_one('asdf')
def test_ansc_raises_if_subcomponent_changed(root): c2 = Component() root.component += c2 c2.changed = True with pytest.raises(batou.UpdateNeeded): root.component.assert_no_subcomponent_changes()
def test_assert_cmd_when_unsuccessful(mockroot): c = Component() c.prepare(mockroot) with pytest.raises(UpdateNeeded): c.assert_cmd('false')
def test_afic_raises_if_file_isolder_than_reference(tmpdir, root): component = Component() root.component += component with pytest.raises(batou.UpdateNeeded): component.assert_file_is_current(__file__, [str(tmpdir)])
def test_assert_no_changes_recursive_raises(root): c2 = Component() root.component += c2 c2.changed = True with pytest.raises(batou.UpdateNeeded): root.component.assert_no_changes()
def test_afic_doesnt_raise_if_file_exists_but_no_reference_is_given(root): component = Component() root.component += component component.assert_file_is_current(__file__)
def test_touch_creates_new_file(tmpdir): reference = str(tmpdir / 'reference') assert not os.path.exists(reference) c = Component() c.touch(reference) assert os.path.exists(reference)
def test_ansc_does_not_raise_if_no_subcomponent_changed(root): c2 = Component() root.component += c2 c2.changed = False root.component.assert_no_subcomponent_changes()