Skip to content

sh_do_test: verbose trace + start to remove inferred output_file#8025

Merged
dlang-bot merged 1 commit intodlang:masterfrom
wilzbach:bash-output_file
Mar 22, 2018
Merged

sh_do_test: verbose trace + start to remove inferred output_file#8025
dlang-bot merged 1 commit intodlang:masterfrom
wilzbach:bash-output_file

Conversation

@wilzbach
Copy link
Copy Markdown
Contributor

Now that a dedicated Bash wrapper exists, a lot of boilerplate code can be removed from the bash scripts.

This is a start which saves an verbose trace of the bash log in .log and all stdout and stddev in output_file. A trap will print both output when an error occurs.

There's a lot more that can be done, see #7928 for ideas.

CC @marler8997 @CyberShadow

@dlang-bot
Copy link
Copy Markdown
Contributor

Thanks for your pull request, @wilzbach!

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

$DMD -c -od=${dir} -Xi=compilerInfo compilable/extra-files/emptymain.d > ${output_file}
rm -f emptymain.json || true
$DMD -c -od=${dir} -Xi=compilerInfo compilable/extra-files/emptymain.d
rm -f emptymain.json
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why || true was added here. rm -f always exits with 0 and also this tests doesn't check the generated output.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cause I didn't know rm -f always returned 0 :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember I was having troubles trying to check the generated output, I can't remember why though...


# dmd should not segfault on -X with libraries, but no source files
out=$("$DMD" -conf= -X foo.a 2>&1)
[ $? -eq 1 ] || exit 1
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's exactly what -e is for. set -euo pipefail should probably be set by default, but that's a task for another PR.


rm -rf "${dir:?}"/"$TEST_NAME"

echo Success
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We no longer need to write sth. to the output file to create it as it's now created by default and only removed in the error case. Of course, that behavior could be easily changed in the sh_do_test script, but the point is that this is superfluous, was often forgotten in the past and should be handled by the wrapper.

cat "${output_file}" 1>&2
fi
rm -f "${output_file}"
}
Copy link
Copy Markdown
Contributor Author

@wilzbach wilzbach Mar 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for custom rolled error handling anymore. This is the default behavior.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depending on how the makefile is written, .DELETE_ON_ERROR might be required.

Copy link
Copy Markdown
Contributor Author

@wilzbach wilzbach Mar 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh this is now mostly handled by the sh_do_test wrapper.
Also removing the output file if an error happened isn't the default behavior because if

  • no input changed, then the output will still be the same
  • it always the developer to inspect the output file manually

cat ${output_file}
rm -f ${output_file}
exit 1
fi
Copy link
Copy Markdown
Contributor Author

@wilzbach wilzbach Mar 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for custom error handling anymore.

@wilzbach wilzbach force-pushed the bash-output_file branch 2 times, most recently from 1ed652e to 786e3de Compare March 13, 2018 15:30
Copy link
Copy Markdown
Contributor

@marler8997 marler8997 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely an improvement :)

@wilzbach
Copy link
Copy Markdown
Contributor Author

Pinging @CyberShadow as you seem to be the only one being able to approve this.

@wilzbach wilzbach added the Merge:72h no objection -> merge The PR will be merged if there are no objections raised. label Mar 18, 2018
@CyberShadow
Copy link
Copy Markdown
Member

Pinging @CyberShadow as you seem to be the only one being able to approve this.

What do you mean? Any committer can approve it.

@wilzbach
Copy link
Copy Markdown
Contributor Author

What do you mean? Any committer can approve it.

Yes, but there aren't so many who feel comfortable with Bash - you and Martin are the only one who I know and Martin is traditionally very busy.
Sorry for the poor wording.

Copy link
Copy Markdown
Member

@CyberShadow CyberShadow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Looks good overall.

name=`basename $0 .sh`
dir=${RESULTS_DIR}/runnable
dmddir=${RESULTS_DIR}${SEP}runnable
output_file=${dir}/${name}.sh.out
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this script wanted to print its output only on failure? Is its output overly verbose, or something?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All output is already printed on a failure, so this is just redundant.


# Test CRLF and mixed line ending handling in D lexer.

set -e
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

I'd go for the whole -euo pipefail trio. Variables that might be unset should be "declared" at the top of the file, i.e. replace unset variables with empty ones.

Copy link
Copy Markdown
Contributor Author

@wilzbach wilzbach Mar 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, my plane is to enable the entire -euo pipefail trio by default for all bash scripts in sh_do_test.sh.
This is just an intermediate PR that should lay the ground work for this.
I just added -e here as it's an almost hard-coded reaction whenever I see || exit 1

dir=${RESULTS_DIR}${SEP}runnable
output_file=${dir}/gdb15729.sh.out

set -e
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flags should probably come right after the shebang, since -u could be added later, and those variable definitions would benefit from -u's checks.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.
FYI: My plan is to add set -eu and maybe even set -o pipefail by default for ALL bash scripts as that would be a sane default.

cat "${output_file}" 1>&2
fi
rm -f "${output_file}"
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depending on how the makefile is written, .DELETE_ON_ERROR might be required.


# redirect stdout + stderr to the output file + keep a reference to the std{out,err} streams for later
exec 40>&1
exec 41>&2
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bash has a syntax to assign a free file descriptor and store it in a variable (exec {name_of_fd_var}>&1), which should guarantee that there will not be collisions, but the feature is "relatively" new and not sure if it's available on all test systems.

Copy link
Copy Markdown
Contributor Author

@wilzbach wilzbach Mar 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! According to this SO answer it's available since available since bash 4.1+ (2009-12-31), so I just gave it a try.
Who knows, maybe it works on all systems

if [ $1 -ne 0 ]; then
echo ==============================
echo Test ${script_name} failed. The logged output:
cat ${output_file}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

General note, would be nice to see a lot more quoting. This line (and many many others) will fail if the variable contains spaces or some other special characters. ShellCheck helps greatly in identifying such moments.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, I think if tests contained spaces, there's alot of places that would fail. But I suppose that's not a good reason to perpetuate bad practice.

exec 2>&41

if [ $1 -ne 0 ]; then
echo ==============================
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

General note, printf > echo in scripts.

@wilzbach wilzbach force-pushed the bash-output_file branch 3 times, most recently from dc811a5 to 1de3cd5 Compare March 22, 2018 16:58
@dlang-bot dlang-bot merged commit ad598ec into dlang:master Mar 22, 2018
@wilzbach wilzbach deleted the bash-output_file branch March 22, 2018 20:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Merge:auto-merge Merge:72h no objection -> merge The PR will be merged if there are no objections raised.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants