def test_run_valid_hash(self):
     self.maxDiff = None
     with patch.object(Runner, 'CHECKBOXES', self.scratch_dir.name),\
             patch('os.execve'):
         with TestIO(combined=True) as io:
             with self.assertRaises(SystemExit) as call:
                 main(['--hash=9ab0e98cce8866b9a2fa217e87b4e8bb'
                       '739e5f74977ba5fa30822cab2a178c48'])
             self.assertEqual(call.exception.args, ('Fatal error',))
         self.assertEqual(io.combined, '')
 def test_run_without_args(self):
     with TestIO(combined=True) as io:
         with self.assertRaises(SystemExit) as call:
             main([])
         self.assertEqual(call.exception.args, (2,))
     expected = """
     usage: checkbox-trusted-launcher [-h] (--hash HASH | --warmup)
                                      [--via LOCAL-JOB-HASH]
                                      [NAME=VALUE [NAME=VALUE ...]]
     checkbox-trusted-launcher: error: one of the arguments --hash --warmup is required
     """
     self.assertEqual(io.combined, cleandoc(expected) + "\n")
 def test_run_valid_hash_invalid_via(self):
     self.maxDiff = None
     with patch.object(Runner, 'CHECKBOXES', self.scratch_dir.name),\
             patch('os.execve'),\
             patch('subprocess.Popen') as mock_popen:
         mock_iobuffer = Mock()
         mock_iobuffer.stdout = StringIO('test: me')
         mock_popen.return_value = mock_iobuffer
         with TestIO(combined=True) as io:
             with self.assertRaises(SystemExit) as call:
                 main(['--hash=9ab0e98cce8866b9a2fa217e87b4e8bb'
                       '739e5f74977ba5fa30822cab2a178c48', '--via=maybe'])
             self.assertEqual(call.exception.args, ('Fatal error',))
         self.assertEqual(io.combined, '')
    def test_help(self):
        with TestIO(combined=True) as io:
            with self.assertRaises(SystemExit) as call:
                main(['--help'])
        self.assertEqual(call.exception.args, (0,))
        self.maxDiff = None
        expected = """
        usage: checkbox-trusted-launcher [-h] (--hash HASH | --warmup)
                                         [--via LOCAL-JOB-HASH]
                                         [NAME=VALUE [NAME=VALUE ...]]

        positional arguments:
          NAME=VALUE            Set each NAME to VALUE in the string environment

        optional arguments:
          -h, --help            show this help message and exit
          --hash HASH           job hash to match
          --warmup              Return immediately, only useful when used with
                                pkexec(1)
          --via LOCAL-JOB-HASH  Local job hash to use to match the generated job
        """
        self.assertEqual(io.combined, cleandoc(expected) + "\n")
 def test_run_invalid_hash(self):
     with TestIO(combined=True) as io:
         with self.assertRaises(SystemExit) as call:
             main(['--hash=bar'])
         self.assertEqual(call.exception.args, ('Job not found',))
     self.assertEqual(io.combined, '')
 def test_warmup(self):
     with TestIO(combined=True) as io:
         with self.assertRaises(SystemExit) as call:
             main(['--warmup'])
         self.assertEqual(call.exception.args, (0,))
     self.assertEqual(io.combined, '')