-
Notifications
You must be signed in to change notification settings - Fork 25
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
Make it POSIX #56
Make it POSIX #56
Conversation
Wrap all code in functions. Use the name as a trigger for actual execution. For slightly improved speed, we actually define `shpec` as a function. This allows us to run shpec as a script or as a function. Naturally, the script just calls the function. To protect the environment all processing occurs inside a subshell.
Not all POSIX shells support `echo -e`. Create a function echoe() to replace every occurrence of `echo -e`.
Split $((( into $(( and ( to show the subexpression better.
`(( ... ))` is not a valid POSIX arithmetic expression. `: $(( ... ))` is ugly, but is equivalent. Tested in ksh93, bash, and dash under Ubuntu 14.04
Add a warning about bash allowing the replacement of any shell builtin. shpec's own tests actually try to redefine `exit`, which is a special builtin in ksh and dash and cannot be redefined.
Sadly, a favourite of mine. But it has to go. Worked fine in bash and ksh, but not in dash. We also split long lines here. Purely cosmetic.
As `shpec` redefines itself as a function internally, we just call that function in the example assertions. No need to locate the actual script. Yay!
That's a greate PR! :) Will try to test it out in the weekend. Could you explain what the hack about arithmetic expression does? (about the if main. i think the way to go in bash is with the |
@hlangeveld: so awesome! |
😍 This looks amazing. It's quite a significant PR, so please give me some time to 1) understand it and 2) provide some feedback. Thanks so much @hlangeveld ! |
On 04/10/15 09:26, Adriean Khisbe wrote:
I think I can. tldr; The hack just takes an assignment masquerading as an expression, but discards the string as Detailed explanation: (Note: In this example, I'll use the markdown convention of using Let's take the very simple expression:
It consists of two parts: The shell builtin As The arithmetic expression actually contains an assignment, and like many modern language, the value
is equivalent to:
with a side-effect of assigning
Note, it is not fully equivalent, because an arithmetic statement will return
will always return true (exit status 0). A precedent can be found in very early *sh documentation;
I believe this was already valid syntax for the Bourne shell, together with:
I hope I made myself clear. Cheers, |
@hlangeveld thanks for the explanation :). |
On 04/11/15 11:39, Adriean Khisbe wrote:
A bit of personal history: Back in '85, I had an instructor who Somehow that stuck with me. Since reading Michael Feathers' '... Legacy Code' Even if you don't actually write a test (but you should), your code will Cheers, |
red="\033[0;31m" | ||
green="\033[0;32m" | ||
norm="\033[0m" | ||
# |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this line have a purpose?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A shell script traditionally started with just ': '. Before kernels interpreted the hasbang,
the shell would see if the first character(s) equalled ':' or ': '. In that case it would fork itself and feed itself the script.
I don't know how I ended up adding an empty comment up there.
@hlangeveld all in all, this is wonderful! I've added a few comments above. Once those are resolved, we just need to update the changelog, bump the version, and merge! |
@hlangeveld now that |
On 04/10/15 18:22, Ryland Herrick wrote:
Thanks for the feedback so far. Most are thinks I need to fix. Cheers, |
On 04/13/15 04:29, Ryland Herrick wrote:
With shpec redefining itself as a function this has little effect on issue #16. The easiest way for me is to put the code executed in the tests in a subshell and test
A completely different way of passing info between the framework and a project's tests would be to make |
Closing this in favor of the newer #58 |
Two major updates:
This removes any '...shisms' not compatible with POSIX. We're not going back to pure Bourne shell, which I consider too primitive. See the individual deltas for what changed. Two major things were:
(( ... ))
expression with: $
.[ ... ]
instead of[[ ... ]]
The second change is to throw all code into functions, this makes testing of functions simple, as we can just source the shpec script and test functions separately. It also simplifies recursive calls to
shpec. However, recursive calls do not update the assertion and failure counters, as its internals
run in a subshell, to protect the environment.
Performance: I had a simple set of 37 assertions which run in ~12 ms on my laptop.
There's little difference between bash, ksh and dash, except that bash takes an extra 8-10 ms to start itself.
I'm still looking for the shell equivalent of python's
if __name__ == '__main__':
Until then I need to trigger on the value of
$0
.