コード例 #1
0
def test_cmdstep_runstep_cmd_is_string_formatting_shell_false():
    """Str command is always not is_save and works with formatting."""
    with patch_logger('blahname', logging.DEBUG) as mock_logger_debug:
        obj = CmdStep('blahname',
                      Context({
                          'k1': 'blah',
                          'cmd': '{k1} -{k1}1 --{k1}2'
                      }))

    assert not obj.is_save
    assert obj.cwd is None
    assert obj.logger.name == 'blahname'
    assert obj.context == Context({'k1': 'blah', 'cmd': '{k1} -{k1}1 --{k1}2'})
    assert obj.cmd_text == 'blah -blah1 --blah2'
    mock_logger_debug.assert_called_once_with(
        'Processing command string: {k1} -{k1}1 --{k1}2')

    with patch('subprocess.run') as mock_run:
        obj.run_step(is_shell=False)

    # blah is in a list because shell == false
    mock_run.assert_called_once_with(['blah', '-blah1', '--blah2'],
                                     cwd=None,
                                     shell=False,
                                     check=True)
コード例 #2
0
def test_cmdstep_runstep_cmd_is_string_formatting_shell_false_sic():
    """Command process special tag directive."""
    with patch_logger('blahname', logging.DEBUG) as mock_logger_debug:
        obj = CmdStep(
            'blahname',
            Context({
                'k1': 'blah',
                'cmd': SicString('{k1} -{k1}1 --{k1}2')
            }))

    assert not obj.is_save
    assert obj.cwd is None
    assert obj.logger.name == 'blahname'
    assert obj.context == Context({
        'k1': 'blah',
        'cmd': SicString('{k1} -{k1}1 --{k1}2')
    })
    assert obj.cmd_text == '{k1} -{k1}1 --{k1}2'
    mock_logger_debug.assert_called_once_with(
        'Processing command string: {k1} -{k1}1 --{k1}2')

    with patch('subprocess.run') as mock_run:
        obj.run_step(is_shell=False)

    # blah is in a list because shell == false
    mock_run.assert_called_once_with(['{k1}', '-{k1}1', '--{k1}2'],
                                     cwd=None,
                                     shell=False,
                                     check=True)
コード例 #3
0
def test_cmdstep_runstep_cmd_is_dict_save_true_shell_false_formatting():
    """Dict command with save false and shell false with formatting."""
    context = Context({
        'k1': 'blah',
        'k2': True,
        'cmd': {
            'run': '{k1} -{k1}1 --{k1}2',
            'save': '{k2}'
        }
    })
    with patch_logger('blahname', logging.DEBUG) as mock_logger_debug:
        obj = CmdStep('blahname', context)

    assert obj.is_save
    assert obj.logger.name == 'blahname'
    assert obj.context == Context({
        'k1': 'blah',
        'k2': True,
        'cmd': {
            'run': '{k1} -{k1}1 --{k1}2',
            'save': '{k2}'
        }
    })
    assert obj.cmd_text == 'blah -blah1 --blah2'
    mock_logger_debug.assert_called_once_with(
        'Processing command string: {k1} -{k1}1 --{k1}2')

    with patch('subprocess.run') as mock_run:
        mock_run.return_value = subprocess.CompletedProcess(
            None, 0, 'std', 'err')
        with patch_logger('blahname', logging.ERROR) as mock_logger_error:
            obj.run_step(is_shell=False)

    mock_logger_error.assert_called_once_with('stderr: err')

    # blah is in a list because shell == false
    mock_run.assert_called_once_with(
        ['blah', '-blah1', '--blah2'],
        cwd=None,
        shell=False,
        # capture_output=True,
        stderr=subprocess.PIPE,
        stdout=subprocess.PIPE,
        # text=True,
        universal_newlines=True)

    assert context['cmdOut']['returncode'] == 0
    assert context['cmdOut']['stdout'] == 'std'
    assert context['cmdOut']['stderr'] == 'err'
コード例 #4
0
def test_cmdstep_runstep_cmd_is_dict_save_true_shell_true_cwd_set():
    """Dict command with save false and shell true with cwd set."""
    context = Context({
        'cmd': {
            'run': 'blah -blah1 --blah2',
            'save': True,
            'cwd': 'pathhere'
        }
    })
    with patch_logger('blahname', logging.DEBUG) as mock_logger_debug:
        obj = CmdStep('blahname', context)

    assert obj.is_save
    assert obj.logger.name == 'blahname'
    assert obj.context == Context({
        'cmd': {
            'run': 'blah -blah1 --blah2',
            'save': True,
            'cwd': 'pathhere'
        }
    })
    assert obj.cmd_text == 'blah -blah1 --blah2'
    mock_logger_debug.assert_called_once_with(
        'Processing command string in dir pathhere: blah -blah1 --blah2')

    with patch('subprocess.run') as mock_run:
        mock_run.return_value = subprocess.CompletedProcess(
            None, 0, 'std', None)
        with patch_logger('blahname', logging.INFO) as mock_logger_info:
            obj.run_step(is_shell=True)

    mock_logger_info.assert_called_once_with('stdout: std')

    # blah is in a list because shell == false
    mock_run.assert_called_once_with(
        'blah -blah1 --blah2',
        cwd='pathhere',
        shell=True,
        # capture_output=True,
        stderr=subprocess.PIPE,
        stdout=subprocess.PIPE,
        # text=True,
        universal_newlines=True)

    assert context['cmdOut']['returncode'] == 0
    assert context['cmdOut']['stdout'] == 'std'
    assert context['cmdOut']['stderr'] is None
コード例 #5
0
def test_cmdstep_runstep_cmd_is_string_shell_true():
    """Str command is always not is_save."""
    with patch_logger('blahname', logging.DEBUG) as mock_logger_debug:
        obj = CmdStep('blahname', Context({'cmd': 'blah -blah1 --blah2'}))

    assert not obj.is_save
    assert obj.logger.name == 'blahname'
    assert obj.context == Context({'cmd': 'blah -blah1 --blah2'})
    assert obj.cmd_text == 'blah -blah1 --blah2'
    mock_logger_debug.assert_called_once_with(
        'Processing command string: blah -blah1 --blah2')

    with patch('subprocess.run') as mock_run:
        obj.run_step(is_shell=True)

    # blah is in a list because shell == false
    mock_run.assert_called_once_with('blah -blah1 --blah2',
                                     cwd=None,
                                     shell=True,
                                     check=True)
コード例 #6
0
def test_cmdstep_runstep_cmd_is_dict_save_false_shell_false():
    """Dict command with save false and shell false."""
    with patch_logger('blahname', logging.DEBUG) as mock_logger_debug:
        obj = CmdStep('blahname',
                      Context({'cmd': {
                          'run': 'blah -blah1 --blah2'
                      }}))

    assert not obj.is_save
    assert obj.logger.name == 'blahname'
    assert obj.context == Context({'cmd': {'run': 'blah -blah1 --blah2'}})
    assert obj.cmd_text == 'blah -blah1 --blah2'
    mock_logger_debug.assert_called_once_with(
        'Processing command string: blah -blah1 --blah2')

    with patch('subprocess.run') as mock_run:
        obj.run_step(is_shell=False)

    # blah is in a list because shell == false
    mock_run.assert_called_once_with(['blah', '-blah1', '--blah2'],
                                     cwd=None,
                                     shell=False,
                                     check=True)
コード例 #7
0
def test_cmdstep_runstep_cmd_is_dict_save_false_shell_true_cwd_formatting():
    """Dict command with save false and shell true, cwd formatting."""
    with patch_logger('blahname', logging.DEBUG) as mock_logger_debug:
        obj = CmdStep(
            'blahname',
            Context({
                'k1': 'v1',
                'k2': 'v2',
                'cmd': {
                    'run': 'blah -blah1 --blah2',
                    'cwd': '/{k1}/{k2}'
                }
            }))

    assert not obj.is_save
    assert obj.cwd == '/v1/v2'
    assert obj.logger.name == 'blahname'
    assert obj.context == Context({
        'k1': 'v1',
        'k2': 'v2',
        'cmd': {
            'run': 'blah -blah1 --blah2',
            'cwd': '/{k1}/{k2}'
        }
    })
    assert obj.cmd_text == 'blah -blah1 --blah2'
    mock_logger_debug.assert_called_once_with(
        'Processing command string in dir /v1/v2: blah -blah1 --blah2')

    with patch('subprocess.run') as mock_run:
        obj.run_step(is_shell=True)

    # blah is in a list because shell == false
    mock_run.assert_called_once_with('blah -blah1 --blah2',
                                     cwd='/v1/v2',
                                     shell=True,
                                     check=True)