File: TestEnviromentSubstitution.cpp

package info (click to toggle)
ecflow 5.15.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,868 kB
  • sloc: cpp: 269,341; python: 22,756; sh: 3,609; perl: 770; xml: 333; f90: 204; ansic: 141; makefile: 70
file content (99 lines) | stat: -rw-r--r-- 4,069 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * Copyright 2009- ECMWF.
 *
 * This software is licensed under the terms of the Apache Licence version 2.0
 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
 * In applying this licence, ECMWF does not waive the privileges and immunities
 * granted to it by virtue of its status as an intergovernmental organisation
 * nor does it submit to any jurisdiction.
 */

#include <iostream>
#include <string>

#include <boost/test/unit_test.hpp>

#include "ecflow/core/Environment.hpp"
#include "ecflow/core/Str.hpp"
#include "ecflow/node/Defs.hpp"
#include "ecflow/node/Suite.hpp"
#include "ecflow/test/scaffold/Naming.hpp"

using namespace std;
using namespace ecf;

BOOST_AUTO_TEST_SUITE(U_Node)

BOOST_AUTO_TEST_SUITE(T_EnvironmentSubstitution)

BOOST_AUTO_TEST_CASE(test_environment_substitution) {
    ECF_NAME_THIS_TEST();

    Defs defs;
    Suite* s = nullptr;
    {
        suite_ptr suite = defs.add_suite("suite");
        s               = suite.get();
        suite->addVariable(Variable("AVI", "avi"));

        std::vector<std::pair<std::string, std::string>> env;
        env.emplace_back(ecf::environment::ECF_HOME, string("/home/smshome"));
        env.emplace_back(string("FRED"), string("/home/fred"));
        env.emplace_back(string("BILL"), string("/home/bill"));
        env.emplace_back(string("JANE"), string("/home/jane"));
        env.emplace_back(string("REP"), string("$REP/bill"));
        defs.server_state().add_or_update_user_variables(env);
    }

    // Check for recursive, in which case we only substitute once
    string expected = "$REP/bill";
    std::string cmd = "$REP";
    BOOST_REQUIRE_MESSAGE(s->variable_dollar_substitution(cmd), " substitution failed");
    BOOST_CHECK_MESSAGE(cmd == expected, "expected '" << expected << "' but found '" << cmd << "'");

    // See page 31, section 5.1 variable inheritance, of SMS users guide
    cmd      = "$ECF_HOME";
    expected = "/home/smshome";
    BOOST_REQUIRE_MESSAGE(s->variable_dollar_substitution(cmd), " substitution failed");
    BOOST_CHECK_MESSAGE(cmd == expected, "expected '" << expected << "' but found '" << cmd << "'");

    cmd      = "$ECF_HOME/include";
    expected = "/home/smshome/include";
    BOOST_REQUIRE_MESSAGE(s->variable_dollar_substitution(cmd), " substitution failed");
    BOOST_CHECK_MESSAGE(cmd == expected, "expected '" << expected << "' but found '" << cmd << "'");

    cmd      = "$ECF_HOME$FRED$BILL$JANE";
    expected = "/home/smshome/home/fred/home/bill/home/jane";
    BOOST_REQUIRE_MESSAGE(s->variable_dollar_substitution(cmd), " substitution failed");
    BOOST_CHECK_MESSAGE(cmd == expected, "expected '" << expected << "' but found '" << cmd << "'");

    cmd      = "$ECF_HOME/$FRED/$BILL/$JANE";
    expected = "/home/smshome//home/fred//home/bill//home/jane";
    BOOST_CHECK_MESSAGE(s->variable_dollar_substitution(cmd), " substitution failed");
    BOOST_CHECK_MESSAGE(cmd == expected, "expected '" << expected << "' but found '" << cmd << "'");

    cmd      = "%PATH";
    expected = "%PATH";
    BOOST_CHECK_MESSAGE(s->variable_dollar_substitution(cmd), " substitution failed");
    BOOST_CHECK_MESSAGE(cmd == expected, "expected '" << expected << "' but found '" << cmd << "'");

    cmd      = "$$";
    expected = "$$";
    BOOST_CHECK_MESSAGE(s->variable_dollar_substitution(cmd), " substitution failed");
    BOOST_CHECK_MESSAGE(cmd == expected, "expected '" << expected << "' but found '" << cmd << "'");

    cmd      = "$ERROR$";
    expected = "$ERROR$";
    BOOST_CHECK_MESSAGE(!s->variable_dollar_substitution(cmd),
                        " substitution expected to fail since ERROR does not exist");
    BOOST_CHECK_MESSAGE(cmd == expected, "expected '" << expected << "' but found '" << cmd << "'");

    cmd      = "";
    expected = "";
    BOOST_CHECK_MESSAGE(s->variable_dollar_substitution(cmd), " substitution failed ");
    BOOST_CHECK_MESSAGE(cmd == expected, "expected '" << expected << "' but found '" << cmd << "'");
}

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE_END()