def test_runcmd_redirects_stdin_from_file(self): fd, _ = tempfile.mkstemp() os.write(fd, 'foobar'.encode()) # send encoded data to stdin os.lseek(fd, 0, os.SEEK_SET) self.assertEqual(cliapp.runcmd_unchecked(['cat'], stdin=fd), (0, b'foobar', b'')) os.close(fd)
def test_runcmd_redirects_stdin_from_file(self): fd, filename = tempfile.mkstemp() os.write(fd, 'foobar') os.lseek(fd, 0, os.SEEK_SET) self.assertEqual(cliapp.runcmd_unchecked(['cat'], stdin=fd), (0, 'foobar', '')) os.close(fd)
def test_runcmd_redirects_stdout_to_file(self): fd, filename = tempfile.mkstemp() exit_code, _, _ = cliapp.runcmd_unchecked(["echo", "foo"], stdout=fd) os.close(fd) with open(filename) as f: data = f.read() self.assertEqual(exit_code, 0) self.assertEqual(data, "foo\n")
def test_runcmd_redirects_stderr_to_file(self): fd, filename = tempfile.mkstemp() exit_code, _, _ = cliapp.runcmd_unchecked(["ls", "notexist"], stderr=fd) os.close(fd) with open(filename) as f: data = f.read() self.assertNotEqual(exit_code, 0) self.assertNotEqual(data, "")
def test_runcmd_unchecked_handles_stdout_err_redirected_to_same_file(self): fd, filename = tempfile.mkstemp() exit_code, _, _ = cliapp.runcmd_unchecked(["sleep", "2"], stdout=fd, stderr=subprocess.STDOUT) os.close(fd) with open(filename) as f: data = f.read() self.assertEqual(exit_code, 0) self.assertEqual(data, "")
def test_runcmd_redirects_stderr_to_file(self): fd, filename = tempfile.mkstemp() exit, out, err = cliapp.runcmd_unchecked(['ls', 'notexist'], stderr=fd) os.close(fd) with open(filename) as f: data = f.read() self.assertNotEqual(exit, 0) self.assertNotEqual(data, '')
def test_runcmd_redirects_stdout_to_file(self): fd, filename = tempfile.mkstemp() exit, out, err = cliapp.runcmd_unchecked(['echo', 'foo'], stdout=fd) os.close(fd) with open(filename) as f: data = f.read() self.assertEqual(exit, 0) self.assertEqual(data, 'foo\n')
def user_xattr_are_supported(self): # Not all filesystems, or mounts of them, support extended # attributes. In order to not fail, we verify that things # work. We do this by checking if we can set a user.foo # attribute with the command line tool. try: exit, out, err = cliapp.runcmd_unchecked(["setfattr", "-n", "user.foo", "bar", self.other]) except OSError: # Either xattr aren't supported, or setfattr isn't # installed and we can't test. return False return exit == 0
def user_xattr_are_supported(self): # Not all filesystems, or mounts of them, support extended # attributes. In order to not fail, we verify that things # work. We do this by checking if we can set a user.foo # attribute with the command line tool. try: exitcode, _, _ = cliapp.runcmd_unchecked( ['setfattr', '-n', 'user.foo', 'bar', self.other]) except OSError: # Either xattr aren't supported, or setfattr isn't # installed and we can't test. return False return exitcode == 0
def test_runcmd_calls_stdout_callback_when_msg_on_stdout(self): msgs = [] def logger(s): msgs.append(s) # We return a string to allow the callback to mangle # the data being returned. return "foo" test_input = "hello fox" _, out, _ = cliapp.runcmd_unchecked(["echo", "-n", test_input], stdout_callback=logger) self.assertEqual(out, "foo") self.assertEqual(msgs, [test_input])
def test_runcmd_calls_stdout_callback_when_msg_on_stdout(self): msgs = [] def logger(s): msgs.append(s) # We return bytes to allow the callback to mangle # the data being returned. return b'foo' test_input = 'hello fox' _, out, _ = cliapp.runcmd_unchecked( ['echo', '-n', test_input], stdout_callback=logger) self.assertEqual(out, b'foo') self.assertEqual(msgs, [test_input.encode('UTF-8')])
def test_runcmd_calls_stderr_callback_when_msg_on_stderr(self): msgs = [] def logger(s): msgs.append(s) # We return None to signal that the data should not be # mangled. return None _, _, err = cliapp.runcmd_unchecked(["ls", "nosuchthing"], stderr_callback=logger) # The callback may be called several times, and we have no # control over that: output from the subprocess may arrive in # drips due to process scheduling and other reasons beyond our # control. Thus, we compare the joined string fragments, # instead of the list of fragments. self.assertNotEqual(err, "") self.assertEqual("".join(msgs), err)
def test_runcmd_returns_stderr_of_command(self): exit, out, err = cliapp.runcmd_unchecked(['ls', 'notexist']) self.assertNotEqual(exit, 0) self.assertEqual(out, '') self.assertNotEqual(err, '')
def test_runcmd_unchecked_returns_values_on_success(self): self.assertEqual(cliapp.runcmd_unchecked(['echo', 'foo']), (0, 'foo\n', ''))
def copylint_is_available(self): returncode, stdout, stderr = cliapp.runcmd_unchecked( ['sh', '-c', 'command -v copyright-statement-lint']) return returncode == 0
def _runcmd_unchecked(self, *args, **kwargs): return cliapp.runcmd_unchecked(*args, cwd=self.dirname, **kwargs)
def runcmd_unchecked(self, *args, **kwargs): return cliapp.runcmd_unchecked(*args, **kwargs)
# # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # # =*= License: GPL-2+ =*= import cliapp def cb(): print('callback called') print('sleeping 10') r = cliapp.runcmd_unchecked( ['sleep', '5'], output_timeout=2, timeout_callback=cb) print(repr(r)) print('done')
def test_runcmd_unchecked_runs_longer_pipeline(self): self.assertEqual(cliapp.runcmd_unchecked(["echo", "foo"], ["cat"], ["wc", "-c"]), (0, "4\n", ""))
def test_runcmd_redirects_stdin_from_file(self): fd, _ = tempfile.mkstemp() os.write(fd, "foobar") os.lseek(fd, 0, os.SEEK_SET) self.assertEqual(cliapp.runcmd_unchecked(["cat"], stdin=fd), (0, "foobar", "")) os.close(fd)
def test_runcmd_unchecked_returns_values_on_failure(self): self.assertEqual(cliapp.runcmd_unchecked(["false"]), (1, "", ""))
def runcmd_unchecked(self, *args, **kwargs): # pragma: no cover return cliapp.runcmd_unchecked(*args, **kwargs)
def test_runcmd_unchecked_returns_values_on_failure(self): self.assertEqual(cliapp.runcmd_unchecked(['false']), (1, '', ''))
def test_runcmd_unchecked_runs_simple_pipeline(self): self.assertEqual( cliapp.runcmd_unchecked(['echo', 'foo'], ['wc', '-c']), (0, b'4\n', b''))
def test_runcmd_unchecked_runs_longer_pipeline(self): self.assertEqual(cliapp.runcmd_unchecked(['echo', 'foo'], ['cat'], ['wc', '-c']), (0, '4\n', ''))
def test_runcmd_returns_stderr_of_command(self): exit_code, out, err = cliapp.runcmd_unchecked(["ls", "notexist"]) self.assertNotEqual(exit_code, 0) self.assertEqual(out, "") self.assertNotEqual(err, "")
def test_runcmd_unchecked_returns_values_on_success(self): self.assertEqual(cliapp.runcmd_unchecked(["echo", "foo"]), (0, "foo\n", ""))
def copylint_is_available(self): returncode, stdout, stderr = cliapp.runcmd_unchecked( ['copyright-statement-lint', '--help']) return returncode == 0