Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add file and directory assertions #115

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

ddidier
Copy link

@ddidier ddidier commented Feb 14, 2019

No description provided.

Copy link
Owner

@kward kward left a comment

Choose a reason for hiding this comment

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

Thank you for the pull!

shunit2 Outdated
shunit_return=${SHUNIT_FALSE}
fi

unset shunit_message_ shunit_file_
Copy link
Owner

Choose a reason for hiding this comment

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

Could you also unset shunit_custom_message_? Same below.

shunit2 Outdated
shunit_message_="${shunit_message_}$shunit_custom_message_"
else
shunit_file_=$1
shunit_message_="${shunit_message_}The file '$shunit_file_' does not exist"
Copy link
Owner

Choose a reason for hiding this comment

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

Wrap $shunit_file in {}. Same below.

shunit2 Outdated
if command [ $# -eq 2 ]; then
shunit_custom_message_=$1
shunit_file_=$2
shunit_message_="${shunit_message_}$shunit_custom_message_"
Copy link
Owner

Choose a reason for hiding this comment

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

Wrap $shunit_custom_message_ in {}. Same below.

shunit2 Outdated
fi

shunit_return=${SHUNIT_TRUE}
if command [ -f "$shunit_file_" ]; then
Copy link
Owner

Choose a reason for hiding this comment

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

Wrap $shunit_file_ in {}, and below.

shunit2 Outdated
fi

shunit_return=${SHUNIT_TRUE}
if command [ -d "$shunit_directory_" ]; then
Copy link
Owner

Choose a reason for hiding this comment

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

{}…

@@ -226,6 +226,13 @@ _th_showOutput() {
unset _th_return_ _th_stdout_ _th_stderr_
}

# Constants used to test file and directory assertions
Copy link
Owner

Choose a reason for hiding this comment

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

As these are only used in the one unit test, they should be present in just that file rather than here. It makes it easier for somebody reading the code to find them. Also, it would make the tests cleaner if the constants were defined explicitly only in the test they were used in.

@@ -226,6 +226,13 @@ _th_showOutput() {
unset _th_return_ _th_stdout_ _th_stderr_
}

# Constants used to test file and directory assertions
TH_EXISTING_DIRECTORY=$(exec 2>/dev/null;cd -- "$(dirname "$0")" || exit; unset PWD; /usr/bin/pwd || /bin/pwd || pwd)
Copy link
Owner

Choose a reason for hiding this comment

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

Could you instead create an explicit directory, rather than using shell to construct one? E.g. "${SHUNIT_TMP}/this_directory_exists".

This makes the test much easier to read, and when a future somebody sees an error with your explicit directory name, they can search for that directory name in the shunit2 code and find the problem much faster.

@@ -226,6 +226,13 @@ _th_showOutput() {
unset _th_return_ _th_stdout_ _th_stderr_
}

# Constants used to test file and directory assertions
TH_EXISTING_DIRECTORY=$(exec 2>/dev/null;cd -- "$(dirname "$0")" || exit; unset PWD; /usr/bin/pwd || /bin/pwd || pwd)
TH_EXISTING_FILE="${TH_EXISTING_DIRECTORY}/$0"
Copy link
Owner

Choose a reason for hiding this comment

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

Same here. Use an explicit filename (name of your choice) inside of ${SHUNIT_TMP}. $0 is dynamic, and one cannot search for the resulting filename in the code and find it, making it more difficult to debug the code.

In general with unit tests, be as explicit as possible.

@@ -246,6 +246,110 @@ testAssertFalse() {
th_assertFalseWithError 'too many arguments' $? "${stdoutF}" "${stderrF}"
}

testAssertFileExists() {
( assertFileExists "$TH_EXISTING_FILE" >"${stdoutF}" 2>"${stderrF}" )
Copy link
Owner

Choose a reason for hiding this comment

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

Rather than using a variable (i.e. ${TH_EXISTING_FILE}), be explicit (see comment later) about which file you are testing for. This makes the code easier to read, and easier to debug.

E.g., if you chose the name 'this_file_exists', then use exactly that filename in the test rather than a variable.

With unit tests, it is best not to "hide" info inside of variables as it makes it more difficult to read the test, and definitely more difficult to debug as one cannot search for the failed strings as easily.

@Andromelus
Copy link

Is it planned to implement it at some point ?

@crramirez
Copy link
Contributor

I am looking forward to this change to be merged

@ddidier
Copy link
Author

ddidier commented Aug 23, 2020

Sorry, I'm not using this anymore and I really don't have any time right know to make the requested changes.

@crramirez
Copy link
Contributor

Ok let's see if I can do them

@ddidier ddidier force-pushed the feature/add_filesystem_assertions branch from f0918a8 to 503c736 Compare November 28, 2020 17:30
@ddidier
Copy link
Author

ddidier commented Nov 28, 2020

I tried to fix this but a lot has changed since the last time. SHUNIT_TMP is not defined in the tests so I had to create my files and directories in oneTimeSetUp. It's quite ugly but I don't know how to do that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants