Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def non_config_init_kwargs_used_as_return_values_for_methods(self):
c = MockContext(run=Result("some output"))
assert c.run("doesn't mattress").stdout == "some output"
def shell_defaults_to_empty_string(self):
assert Result().shell == ""
def return_value_kwargs_can_take_iterables_too(self):
c = MockContext(run=[Result("some output"), Result("more!")])
assert c.run("doesn't mattress").stdout == "some output"
assert c.run("still doesn't mattress").stdout == "more!"
run_results = {
# Branch detection
"git rev-parse --abbrev-ref HEAD": Result(self._branch),
# Changelog update action - just here so it can be called
"$EDITOR {0.packaging.changelog_file}".format(config): Result(),
# Version file update - ditto
"$EDITOR {0}/_version.py".format(FAKE_PACKAGE): Result(),
# Git tags
"git tag": Result(tag_output),
# Git status/commit/tagging
# TODO: yea I'd really like regexen now plz sigh
"git tag 1.1.2": Result(""),
'git commit -am "Cut 1.1.2"': Result(""),
# NOTE: some tests will need to override this, for now default to a
# result that implies a commit is needed
'git status --porcelain | egrep -v "^\\?"': Result(
"M somefile", exited=0
),
}
context = MockContext(config=config, run=run_results)
# Wrap run() in a Mock too.
# NOTE: we don't do this inside MockContext itself because that would add a
# test lib as a runtime dependency =/
# NOTE: end-running around Context/DataProxy setattr because doing
# context.run.echo = True (or similar) is too common a use case to be worth
# breaking just for stupid test monkeypatch purposes
object.__setattr__(context, "run", Mock(wraps=context.run))
#
# Execute converge() inside a mock environment
#
def stderr_defaults_to_empty_string(self):
assert Result().stderr == u""
)
tag_output = ""
if hasattr(self, "_tags"):
tag_output = "\n".join(self._tags) + "\n"
# TODO: if/when regex implemented for MockContext, make these keys less
# strictly tied to the real implementation.
# NOTE: Result first posarg is stdout string data.
run_results = {
# Branch detection
"git rev-parse --abbrev-ref HEAD": Result(self._branch),
# Changelog update action - just here so it can be called
"$EDITOR {0.packaging.changelog_file}".format(config): Result(),
# Version file update - ditto
"$EDITOR {0}/_version.py".format(FAKE_PACKAGE): Result(),
# Git tags
"git tag": Result(tag_output),
# Git status/commit/tagging
# TODO: yea I'd really like regexen now plz sigh
"git tag 1.1.2": Result(""),
'git commit -am "Cut 1.1.2"': Result(""),
# NOTE: some tests will need to override this, for now default to a
# result that implies a commit is needed
'git status --porcelain | egrep -v "^\\?"': Result(
"M somefile", exited=0
),
}
context = MockContext(config=config, run=run_results)
# Wrap run() in a Mock too.
# NOTE: we don't do this inside MockContext itself because that would add a
# test lib as a runtime dependency =/
# NOTE: end-running around Context/DataProxy setattr because doing
# context.run.echo = True (or similar) is too common a use case to be worth
# TODO: if/when regex implemented for MockContext, make these keys less
# strictly tied to the real implementation.
# NOTE: Result first posarg is stdout string data.
run_results = {
# Branch detection
"git rev-parse --abbrev-ref HEAD": Result(self._branch),
# Changelog update action - just here so it can be called
"$EDITOR {0.packaging.changelog_file}".format(config): Result(),
# Version file update - ditto
"$EDITOR {0}/_version.py".format(FAKE_PACKAGE): Result(),
# Git tags
"git tag": Result(tag_output),
# Git status/commit/tagging
# TODO: yea I'd really like regexen now plz sigh
"git tag 1.1.2": Result(""),
'git commit -am "Cut 1.1.2"': Result(""),
# NOTE: some tests will need to override this, for now default to a
# result that implies a commit is needed
'git status --porcelain | egrep -v "^\\?"': Result(
"M somefile", exited=0
),
}
context = MockContext(config=config, run=run_results)
# Wrap run() in a Mock too.
# NOTE: we don't do this inside MockContext itself because that would add a
# test lib as a runtime dependency =/
# NOTE: end-running around Context/DataProxy setattr because doing
# context.run.echo = True (or similar) is too common a use case to be worth
# breaking just for stupid test monkeypatch purposes
object.__setattr__(context, "run", Mock(wraps=context.run))
#
def run(self):
mc = MockContext(run={"foo": Result("bar")})
assert mc.run("foo").stdout == "bar"
mc.set_result_for("run", "foo", Result("biz"))
assert mc.run("foo").stdout == "biz"
def return_value_is_Result_subclass_exposing_cxn_used(self, remote):
c = _Connection("host")
r = Remote(context=c)
result = r.run(CMD)
assert isinstance(result, Result)
# Mild sanity test for other Result superclass bits
assert result.ok is True
assert result.exited == 0
# Test the attr our own subclass adds
assert result.connection is c
websocket.send_message(webuser, m)
return result
except Exception as e:
message = '[%s@%s]%s' % (self.user, self.host, e)
error_logger.error(message)
message = '[%s@%s]# %s\n[ERROR] %s' % (self.user, self.host, command, str(e))
if write:
with open(write, 'a') as f:
f.write(message)
elif webuser:
message_in = '[%s@%s]# %s' % (self.user, self.host, command)
message_out = '[ERROR] %s' % (str(e))
Tailf.send_message(webuser, message_in)
for m in message_out.split('\n'):
Tailf.send_message(webuser, m)
result = Result(exited=-1, stderr=message, stdout=message)
return result