It's all about the answers!

Ask a question

Verifying a variable has been set...


Chad Stanke (61) | asked Sep 09 '10, 8:40 p.m.
Hi all,

New BF user here. I've scoured the docs and can't find an answer so I'm hoping the Jazz community can help.

Does BuildForge have a way to verify a variable has a value (like bash's 'test -n', perl's 'defined()', and php's 'empty()')?

Currently, when I want to verify a variable (that may or may not have been set by a .tset), I create a verify step that runs on a Linux or Mac box to check the values of the variables we expect to be able to use in bash. So I have a step that .tsets the variable and inlines a library. In addition to its main work, the library verifies the variable has a value - failing if it doesn't and continuing if it does. The verification step is something like this:

if [ "${MY_PARENT_DIR}" != "MY_PARENT_DIR" ]; then

echo VARTEST: [STATUS=PASS]MY_PARENT_DIR = ${MY_PARENT_DIR}[/STATUS]
else
echo VARTEST: [STATUS=FAIL]MY_PARENT_DIR = ${MY_PARENT_DIR}[/STATUS]
exit 1
fi


Since BF replaces the variable value with the variable name if the var isn't set, this is a good way to check if it's set. The world is fine and good until the variable has a Windows path separator in it. In this case, the verification step chokes depending on what the Win path sep is "escaping." For instance, if the variable contains a windows directory name with a trailing slash:
"MyDir\"
then the variable value is
MyDir"
(note the trailing " where the slash escaped it). Bash isn't happy with that at all so I need to find a more stable way of doing this.

If BuildForge doesn't have anything built-in, I imagine someone has solved this in a robust and repeatable way. Let me know what you've done.
:)

2 answers



permanent link
Brent Ulbricht (2.5k11) | answered Sep 13 '10, 11:49 a.m.
JAZZ DEVELOPER
Hi all,

New BF user here. I've scoured the docs and can't find an answer so I'm hoping the Jazz community can help.

Does BuildForge have a way to verify a variable has a value (like bash's 'test -n', perl's 'defined()', and php's 'empty()')?

Currently, when I want to verify a variable (that may or may not have been set by a .tset), I create a verify step that runs on a Linux or Mac box to check the values of the variables we expect to be able to use in bash. So I have a step that .tsets the variable and inlines a library. In addition to its main work, the library verifies the variable has a value - failing if it doesn't and continuing if it does. The verification step is something like this:

if [ "${MY_PARENT_DIR}" != "MY_PARENT_DIR" ]; then

echo VARTEST: [STATUS=PASS]MY_PARENT_DIR = ${MY_PARENT_DIR}[/STATUS]
else
echo VARTEST: [STATUS=FAIL]MY_PARENT_DIR = ${MY_PARENT_DIR}[/STATUS]
exit 1
fi


Since BF replaces the variable value with the variable name if the var isn't set, this is a good way to check if it's set. The world is fine and good until the variable has a Windows path separator in it. In this case, the verification step chokes depending on what the Win path sep is "escaping." For instance, if the variable contains a windows directory name with a trailing slash:
"MyDir\"
then the variable value is
MyDir"
(note the trailing " where the slash escaped it). Bash isn't happy with that at all so I need to find a more stable way of doing this.

If BuildForge doesn't have anything built-in, I imagine someone has solved this in a robust and repeatable way. Let me know what you've done.
:)


Hi,

There are a couple of things I can think of that you might try. I think the first option may require less refactor of your steps.

Build Forge can run Perl code that you've specified in the command text of a step if you use the #! directive at the top of the step and the location of the perl to use. For example, the following command text checks if a variable is defined. The one trick that I've used in this is to set the environment variable _NO_PREPARSE_COMMAND=1 for only this step so that the $ENV does not cause Build Forge to try to resolve it.

Step Command Text

#!C:\Perl\bin\perl.exe

if (defined($ENV{'TEST_VAR_1'})) {
print "Yes, the variable is defined. TEST_VAR_1 = " . $ENV{'TEST_VAR_1'} . "\n";
} else {
print "No, the variable TEST_VAR_1 is not defined!\n";
exit 1;
}

This is the output of the step log.


166 9/13/10 10:20 AM ENV _NO_PREPARSE_COMMAND=1
167 9/13/10 10:20 AM SCRIPT #!C:\Perl\bin\perl.exe
168 9/13/10 10:20 AM SCRIPT
169 9/13/10 10:20 AM SCRIPT if (defined($ENV{'TEST_VAR_1'})) {
170 9/13/10 10:20 AM SCRIPT print "Yes, the variable is defined. TEST_VAR_1 = " . $ENV{'TEST_VAR_1'} . "\n";
171 9/13/10 10:20 AM SCRIPT } else {
172 9/13/10 10:20 AM SCRIPT print "No, the variable TEST_VAR_1 is not defined!\n";
173 9/13/10 10:20 AM SCRIPT exit 1;
174 9/13/10 10:20 AM SCRIPT }
175 9/13/10 10:20 AM EXEC start
176 9/13/10 10:20 AM EXEC Yes, the variable is defined. TEST_VAR_1 = ORIGINAL_VALUE
177 9/13/10 10:20 AM EXEC end



This other option you could try might involve a few rearrangements of steps. This idea is to use an environment group set on a step with a variable having an action of 'Set if not set'. If the .tset did not set this variable, then you could have the 'Set if not set' go to a value that is an indicator that the variable wasn't defined (value = NOT_DEFINED).

Then you could have another step with a step type of 'Conditional' that could check if the known value for not being set was specified do this else do that .

bju

permanent link
Chad Stanke (61) | answered Oct 11 '10, 12:43 p.m.

Build Forge can run Perl code that you've specified in the command text of a step if you use the #! directive at the top of the step and the location of the perl to use. For example, the following command text checks if a variable is defined. The one trick that I've used in this is to set the environment variable _NO_PREPARSE_COMMAND=1 for only this step so that the $ENV does not cause Build Forge to try to resolve it.

Step Command Text

#!C:\Perl\bin\perl.exe

if (defined($ENV{'TEST_VAR_1'})) {
print "Yes, the variable is defined. TEST_VAR_1 = " . $ENV{'TEST_VAR_1'} . "\n";
} else {
print "No, the variable TEST_VAR_1 is not defined!\n";
exit 1;
}

This is the output of the step log.


166 9/13/10 10:20 AM ENV _NO_PREPARSE_COMMAND=1
167 9/13/10 10:20 AM SCRIPT #!C:\Perl\bin\perl.exe
168 9/13/10 10:20 AM SCRIPT
169 9/13/10 10:20 AM SCRIPT if (defined($ENV{'TEST_VAR_1'})) {
170 9/13/10 10:20 AM SCRIPT print "Yes, the variable is defined. TEST_VAR_1 = " . $ENV{'TEST_VAR_1'} . "\n";
171 9/13/10 10:20 AM SCRIPT } else {
172 9/13/10 10:20 AM SCRIPT print "No, the variable TEST_VAR_1 is not defined!\n";
173 9/13/10 10:20 AM SCRIPT exit 1;
174 9/13/10 10:20 AM SCRIPT }
175 9/13/10 10:20 AM EXEC start
176 9/13/10 10:20 AM EXEC Yes, the variable is defined. TEST_VAR_1 = ORIGINAL_VALUE
177 9/13/10 10:20 AM EXEC end


bju

Thanks Brent! This worked great (as long as I set _NO_PREPARSE_COMMAND=1). My step ended up looking like this:


#!/usr/bin/perl


# Verify variable values using embedded PERL
# Pass plaintext variable name strings (not the variables themselves) to "VerifyVariable()."
VerifyVariable("TEST_VAR_1");
VerifyVariable("TEST_VAR_2");



sub VerifyVariable()
{
my $test_var = shift;
my $exit_value = 0; # Assume success
if(defined($ENV{$test_var}))
{
print("VARTEST: " . '[STATUS=PASS]' . "$test_var = $ENV{$test_var}" . '[/STATUS]' . "\n");
$exit_value = 0;
}
else
{
print("VARTEST: " . '[STATUS=FAIL]' . "$test_var = $test_var" . '[/STATUS]' . "\n");
$exit_value = 1;
}
exit $exit_value;
}


It's clean, consistent, and simple. This is nearly exactly what I was looking for. Thanks for your reply and useful info!

Your answer


Register or to post your answer.


Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.