def test_param_a_to_script(stc):
    stc_sys = CStcSystem.Instance()
    project = stc_sys.GetObject('Project')
    sequencer = stc_sys.GetObject("Sequencer")
    ctor = CScriptableCreator()
    pkg = "spirent.methodology"

    tags = project.GetObject('Tags')
    tagA = ctor.Create("tag", tags)
    tagA.Set("Name", "tagA")

    cmd = ctor.Create(pkg + ".RunPyScriptCommand", sequencer)
    RunPyScriptCommand.get_this_cmd = MagicMock(return_value=cmd)

    cmd2 = ctor.Create(pkg + ".LoadTemplateCommand", sequencer)
    cmd2.AddObject(tagA, RelationType("UserTag"))

    script_folder = mmutils.get_scripts_home_dir()
    filename = os.path.join(script_folder, "unit_test_file_param_a_to_script.py")
    try:
        if not os.path.exists(script_folder):
            os.makedirs(script_folder)
        with open(filename, "w") as f:
            f.write(unit_test_file_param_a_to_script())
        RunPyScriptCommand.run("unit_test_file_param_a_to_script", "run", "tagA", {'a': 4})
        assert cmd2.Get("CopiesPerParent") == 4
    finally:
        remove_files(filename)
    return
def hold_test_error_returned(stc):
    stc_sys = CStcSystem.Instance()
    project = stc_sys.GetObject('Project')
    sequencer = stc_sys.GetObject("Sequencer")
    ctor = CScriptableCreator()
    pkg = "spirent.methodology"

    tags = project.GetObject('Tags')
    tagA = ctor.Create("tag", tags)
    tagA.Set("Name", "tagA")

    cmd = ctor.Create(pkg + ".RunPyScriptCommand", sequencer)
    RunPyScriptCommand.get_this_cmd = MagicMock(return_value=cmd)

    script_folder = mmutils.get_scripts_home_dir()
    filename = os.path.join(script_folder, "unit_test_file_error_returned.py")
    try:
        if not os.path.exists(script_folder):
            os.makedirs(script_folder)
        with open(filename, "w") as f:
            f.write(unit_test_file_error_returned())
        rc = RunPyScriptCommand.run("unit_test_file_error_returned", "errorout", "tagA", {})
        assert rc is False
        assert cmd.Get("ErrorMsg") != ""
    finally:
        remove_files(filename)
    return
def test_validate_missing_file():
    stc_sys = CStcSystem.Instance()
    sequencer = stc_sys.GetObject("Sequencer")
    ctor = CScriptableCreator()
    pkg = "spirent.methodology"

    cmd = ctor.Create(pkg + ".RunPyScriptCommand", sequencer)
    RunPyScriptCommand.get_this_cmd = MagicMock(return_value=cmd)
    err = RunPyScriptCommand.validate("unit_test_file_missing_script", "run", "tagA", "")
    assert err != ""
def test_validate_valid_file():
    stc_sys = CStcSystem.Instance()
    sequencer = stc_sys.GetObject("Sequencer")
    ctor = CScriptableCreator()
    pkg = "spirent.methodology"

    cmd = ctor.Create(pkg + ".RunPyScriptCommand", sequencer)
    RunPyScriptCommand.get_this_cmd = MagicMock(return_value=cmd)

    script_folder = mmutils.get_scripts_home_dir()
    filename = os.path.join(script_folder, "unit_test_file_valid_script.py")
    try:
        if not os.path.exists(script_folder):
            os.makedirs(script_folder)
        with open(filename, "w") as f:
            f.write(unit_test_file_valid_script())
        err = RunPyScriptCommand.validate("unit_test_file_valid_script", "run", "tagA", "")
        assert err == ""
        err = RunPyScriptCommand.validate("unit_test_file_valid_script.py", "run", "tagA", "")
        assert err == ""
    finally:
        remove_files(filename)
    return