-
-
Notifications
You must be signed in to change notification settings - Fork 32
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
feature: remove curl perl and coreutils as required dependencies #345
Merged
Chemaclass
merged 7 commits into
TypedDevs:main
from
skinner-m-c:feature/remove-dependencies
Oct 1, 2024
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ab451be
feature: remove curl perl and coreutils as required dependencies
skinner-m-c f329d33
Merge branch 'main' into feature/remove-dependencies
Chemaclass bd44743
fix: windows test on mac
Chemaclass 16d1396
fix: test_render_execution_time_on_osx_without_perl
Chemaclass 6cbeb1e
test: skip osx unit tests on windows
Chemaclass f22cf75
fix: test_render_execution_time_on_osx_without_perl
Chemaclass 54966e5
fix: mock _START_TIME on test_render_execution_time_on_osx_without_perl
Chemaclass File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,57 @@ | ||
#!/bin/bash | ||
|
||
function clock::now() { | ||
if perl -MTime::HiRes -e "" > /dev/null 2>&1; then | ||
perl -MTime::HiRes -e 'printf("%.0f\n", Time::HiRes::time() * 1000)' | ||
elif [[ "${_OS:-}" != "OSX" ]]; then | ||
if dependencies::has_perl && perl -MTime::HiRes -e "" > /dev/null 2>&1; then | ||
if perl -MTime::HiRes -e 'printf("%.0f\n",Time::HiRes::time()*1000000000)'; then | ||
return 0 | ||
fi | ||
fi | ||
|
||
if ! check_os::is_macos && ! check_os::is_alpine; then | ||
date +%s%N | ||
else | ||
echo "" | ||
return 0 | ||
fi | ||
|
||
local shell_time has_shell_time | ||
shell_time="$(clock::shell_time)" | ||
has_shell_time="$?" | ||
if [[ "$has_shell_time" -eq 0 ]]; then | ||
local seconds microseconds | ||
seconds=$(echo "$shell_time" | cut -f 1 -d '.') | ||
microseconds=$(echo "$shell_time" | cut -f 2 -d '.') | ||
|
||
math::calculate "($seconds * 1000000000) + ($microseconds * 1000)" | ||
return 0 | ||
fi | ||
|
||
echo "" | ||
return 1 | ||
} | ||
|
||
function clock::shell_time() { | ||
# Get time directly from the shell rather than a program. | ||
[[ -n ${EPOCHREALTIME+x} && -n "$EPOCHREALTIME" ]] && LC_ALL=C echo "$EPOCHREALTIME" | ||
} | ||
|
||
_START_TIME=$(clock::now) | ||
|
||
function clock::total_runtime_in_milliseconds() { | ||
end_time=$(clock::now) | ||
if [[ -n $end_time ]]; then | ||
echo $(( end_time - _START_TIME )) | ||
math::calculate "($end_time-$_START_TIME)/1000000" | ||
else | ||
echo "" | ||
fi | ||
} | ||
|
||
function clock::total_runtime_in_nanoseconds() { | ||
end_time=$(clock::now) | ||
if [[ -n $end_time ]]; then | ||
math::calculate "($end_time-$_START_TIME)" | ||
else | ||
echo "" | ||
fi | ||
} | ||
|
||
function clock::init() { | ||
_START_TIME=$(clock::now) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/bin/bash | ||
set -euo pipefail | ||
|
||
function dependencies::has_perl() { | ||
command -v perl >/dev/null 2>&1 | ||
} | ||
|
||
function dependencies::has_adjtimex() { | ||
command -v adjtimex >/dev/null 2>&1 | ||
} | ||
|
||
function dependencies::has_bc() { | ||
command -v bc >/dev/null 2>&1 | ||
} | ||
|
||
function dependencies::has_awk() { | ||
command -v awk >/dev/null 2>&1 | ||
} | ||
|
||
function dependencies::has_git() { | ||
command -v git >/dev/null 2>&1 | ||
} | ||
|
||
function dependencies::has_curl() { | ||
command -v curl >/dev/null 2>&1 | ||
} | ||
|
||
function dependencies::has_wget() { | ||
command -v wget >/dev/null 2>&1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/bin/bash | ||
|
||
function io::download_to() { | ||
local url="$1" | ||
local output="$2" | ||
if dependencies::has_curl; then | ||
curl -L -J -o "$output" "$url" 2>/dev/null | ||
elif dependencies::has_wget; then | ||
wget -q -O "$output" "$url" 2>/dev/null | ||
else | ||
return 1 | ||
fi | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/bin/bash | ||
|
||
if dependencies::has_bc; then | ||
# bc is better than awk because bc has no integer limits. | ||
function math::calculate() { | ||
echo "$*" | bc | ||
} | ||
elif dependencies::has_awk; then | ||
function math::calculate() { | ||
awk "BEGIN { print ""$*"" }" | ||
} | ||
fi | ||
Chemaclass marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
You might have notice we are using everywhere in the project checking true|false for conditional booleans. However, they are raw strings are bools are not natively supported by bash. On the contrary, native conditions in bash are
0:true,>=1:false
which is a bit odd if you come from other languages. In the end this is purely a detail implementation from the bashunit insides, so it should not matter to the external user.I am saying this because I am wondering if there is a performance gain/benefit on using 0/1 for booleans intead of true/false in which case I would be in favor of refactoring the bools inside the project to use them. Otherwise, I would suggest keep using true/false. What do you think, @skinner-m-c ?
Anyway, THIS comment should NOT be applied on this PR but in a follow up iteration.
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.
I agree that it is confusing that
0
beingtrue
and1
beingfalse
is confusing. However it comes from Unix standard of program exit codes -- that use0
meaning "normal exit and not errors" and anything else is an exit where the program executed with some problems.true
andfalse
are programs, typically installed at/bin/true
and/bin/false
. They are provided bycoreutils
orbusybox
. Both of these programs are very simple and are unlikely to cause performance issues. There may be more of a performance hit starting these programs than their execution. See true.c in coreutils and busybox for what they actually do. So it is probably faster to use0
and1
for comparisons. Whentrue
andfalse
are returned it is the output of these programs.However, BASH also has built-in functions and that is what is actually being used here and so
/bin/true
and/bin/false
is not actually being run. The built-intrue
andfalse
is probably marginally slower than integers (seehelp
in BASH to see the list of all built-in functions). I don't have any metric to support that idea, but it is probably too small to warrant choosing0
overfalse
for example.false
andtrue
also are more readable.I agree that
true
andfalse
should probably be used.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.
In such a case, do you mind creating a PR refactoring/changing this to use true/false then? Then we can create an ADR to write down this as a project convention, so we can keep consistency over the project when dealing with booleans.
Extra thought: it's not the same
true
and "true", right? one is a program and the other one is a string, I assume, but I believe they are compatible and working intermixed? anyway, that's why the ADR is important to keep consistency.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.
I've been thinking about this topic, and while I think it's a bit odd thinking this way if you are not familiar with sh/bash, and after more than a year working on the project and a lot of researching, I ended up believing that 0 (as true) and 1 (as false) is a good practice in bash itself, specially when defining conditions.
While you can do this in bash
but you cannot do this:
TL;DR I would be in favor of using 0 (as true) and 1 (as false) when dealing with conditionals in the project. I will create an ADR for this and refactor the project to follow this convention.
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.
Follow up: #346
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.
This also gave me the idea to add assert_true and assert_false #350
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.
Glad it is inspiring some more improvements.
true
andfalse
are build-in functions in BASH. Functions in BASH cannot be returned (though you can return a string of a function name and then invoke it witheval
). However, the return value of the last function is returned ifreturn
has no value. So it would be a two-liner or a one liner with a subshell. Subshells should be avoided if possible because it is slower.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.
Do you mind reviewing the last pr where I introduced assert_true and false? I would like your feedback:)