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

Better temp dir and output #430

Closed
wants to merge 31 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
a2b6d41
use varname temporary_filename, not filename
joshrabinowitz Mar 19, 2019
1d9cad5
improve output
joshrabinowitz Mar 20, 2019
42291f5
use just 'tempdir'
joshrabinowitz Mar 20, 2019
1aa6a19
reset TMPDIR
joshrabinowitz Mar 20, 2019
de9558c
logic change: .git dir not needed for usage
joshrabinowitz Mar 20, 2019
7309249
small change
joshrabinowitz Mar 20, 2019
ef2d8b8
add diagnostic output to debug tempdir issue
joshrabinowitz Mar 20, 2019
f961ebe
Merge branch 'master' into better-temp-dir
joshrabinowitz Mar 20, 2019
4981592
remove test/debug code
joshrabinowitz Mar 20, 2019
823de66
fix typo in comment
joshrabinowitz Mar 20, 2019
a38e7d6
add comment about bug in _get_git_root_path
joshrabinowitz Mar 20, 2019
3fff8a9
more debug output
joshrabinowitz Mar 20, 2019
a4477f4
use /tmp/tempdir as test directory
joshrabinowitz Mar 20, 2019
0ce0e54
remove debug output
joshrabinowitz Mar 20, 2019
167687d
remove debug output
joshrabinowitz Mar 20, 2019
3bf3d3e
add a test and debug output
joshrabinowitz Mar 20, 2019
06a64fd
remove test that is duplicated in test_usage.bats
joshrabinowitz Mar 20, 2019
82c67ab
restore test, expect usage to fail if no .git dir
joshrabinowitz Mar 20, 2019
93ca5ec
add comment
joshrabinowitz Mar 20, 2019
5b2a5a3
trap outputs to stderr, not fd3
joshrabinowitz Mar 20, 2019
207a3c4
use 'git check-ignore', simplify code.
joshrabinowitz Mar 20, 2019
ca58b04
'usage' can succeed even if repo isn't configured
joshrabinowitz Mar 20, 2019
1e55f31
change tests to match code, and add debug output
joshrabinowitz Mar 20, 2019
5de8fc0
fix quoting, remove unneeded code.
joshrabinowitz Mar 20, 2019
b1ebe0b
add diagnostic output
joshrabinowitz Mar 20, 2019
924f76d
rename test so it works with our use of sed
joshrabinowitz Mar 20, 2019
08877df
expect usage to fail if other commands can't run
joshrabinowitz Mar 20, 2019
6f44f0d
code cleanup, remove debug output, tighten tests
joshrabinowitz Mar 20, 2019
5aa684b
Merge branch 'master' into better-temp-dir
joshrabinowitz Mar 21, 2019
92a0860
Merge branch 'master' into better-temp-dir
joshrabinowitz Mar 22, 2019
6c98cef
show debug info around failures in #430
joshrabinowitz Mar 23, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,6 @@ build/
*.deb
*.fpm

# Kithcne files
# Kitchen files
Gemfile.lock
.kitchen/
37 changes: 12 additions & 25 deletions src/_utils/_git_secret_tools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,14 @@ function _file_has_line {



# this sets the global variable 'filename'
# currently this function is only used by 'hide'
# this sets the global variable 'temporary_filename'
# currently this function is only used by 'hide' and below in _gawk_inplace
function _temporary_file {
# This function creates temporary file
# which will be removed on system exit.
filename=$(_os_based __temp_file) # is not `local` on purpose.
temporary_filename=$(_os_based __temp_file) # is not `local` on purpose.

trap 'echo "git-secret: cleaning up..."; rm -f "$filename";' EXIT
trap 'echo "git-secret: cleaning up: $temporary_filename"; rm -f "$temporary_filename";' EXIT
}


Expand All @@ -208,10 +208,10 @@ function _gawk_inplace {
local dest_file
dest_file="$(echo "$parms" | gawk -v RS="'" -v FS="'" 'END{ gsub(/^\s+/,""); print $1 }')"

_temporary_file
_temporary_file # sets $temporary_filename

bash -c "gawk ${parms}" > "$filename"
mv "$filename" "$dest_file"
bash -c "gawk ${parms}" > "$temporary_filename"
mv "$temporary_filename" "$dest_file"
}


Expand Down Expand Up @@ -292,14 +292,7 @@ function _check_ignore {
local filename="$1" # required

local result
result="$(git add -n "$filename" > /dev/null 2>&1; echo $?)"
# when ignored
if [[ "$result" -ne 0 ]]; then
result=0
else
result=1
fi
# returns 1 when not ignored, and 0 when ignored
result="$(git check-ignore -q "$filename"; echo $?)"
echo "$result"
}

Expand Down Expand Up @@ -361,6 +354,9 @@ function _is_tracked_in_git {
}


# THIS CAN GIVE WRONG .git DIR IF WE ARE TESTING IN A SUBDIR of src/git-secret.
# for example, if we're testing in ~/src/git-secret/tempdir and there's no .git dir in that directory,
# this function will return ~/src/git-secret/ -- WHICH IS THE WRONG DIR
function _get_git_root_path {
# We need this function to get the location of the `.git` folder,
# since `.gitsecret` (or value set by SECRETS_DIR env var) must be on the same level.
Expand Down Expand Up @@ -476,7 +472,7 @@ function _find_and_clean_formatted {
local pattern="$1" # can be any string pattern

if [[ -n "$_SECRETS_VERBOSE" ]]; then
echo && echo "git-secret: cleaning:"
echo && _message "cleaning:"
fi

_find_and_clean "$pattern"
Expand Down Expand Up @@ -527,17 +523,8 @@ function _secrets_dir_is_not_ignored {
local git_secret_dir
git_secret_dir=$(_get_secrets_dir)

# Create git_secret_dir required for check
local cleanup=0
if [[ ! -d "$git_secret_dir" ]]; then
mkdir "$git_secret_dir"
cleanup=1
fi
local ignores
ignores=$(_check_ignore "$git_secret_dir")
if [[ "$cleanup" == 1 ]]; then
rmdir "$git_secret_dir"
fi

if [[ ! $ignores -eq 1 ]]; then
_abort "'$git_secret_dir' is in .gitignore"
Expand Down
2 changes: 1 addition & 1 deletion src/commands/git_secret_init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function init {
mkdir "$git_secret_dir" "$(_get_secrets_dir_keys)" "$(_get_secrets_dir_path)"
touch "$(_get_secrets_dir_keys_mapping)" "$(_get_secrets_dir_paths_mapping)"

_message "'$git_secret_dir/' created."
_message "init created: '$git_secret_dir/'"

local random_seed_file
random_seed_file="${_SECRETS_DIR}/keys/random_seed"
Expand Down
6 changes: 3 additions & 3 deletions src/commands/git_secret_tell.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ function tell {
local start_key_cnt
start_key_cnt=$(get_gpg_key_count)
for email in "${emails[@]}"; do
# This file will be removed automatically:
_temporary_file # note, that `_temporary_file` will export `filename` var.
# This temporary file will be removed automatically:
_temporary_file # note that `_temporary_file` will set `temporary_filename` var.
# shellcheck disable=2154
local keyfile="$filename"
local keyfile="$temporary_filename"

local exit_code
if [[ -z "$homedir" ]]; then
Expand Down
13 changes: 11 additions & 2 deletions tests/_test_base.bash
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,18 @@ function get_gpg_fingerprint_by_email {
function install_fixture_key {
local public_key="$BATS_TMPDIR/public-${1}.key"

echo "# in install_fixture_key: exporting public_key to $public_key" >&3

cp "$FIXTURES_DIR/gpg/${1}/public.key" "$public_key"
$GPGTEST --import "$public_key" > /dev/null 2>&1
rm -f "$public_key"
local import_output
import_output=$($GPGTEST --verbose --import "$public_key" 2>&1)

echo "# output of key import: $import_output" >&3
if [ -f "$public_key" ]; then
rm -f "$public_key"
else
echo "# in install_fixture_key: can't find exported public key: $public_key" >&3
fi
}


Expand Down
4 changes: 2 additions & 2 deletions tests/test_hide.bats
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ function teardown {
# git secret hide -m, use temp file so cleaning should take place
[[ "${#lines[@]}" -eq 2 ]]
[ "${lines[0]}" = "git-secret: done. 1 of 1 files are hidden." ]
[ "${lines[1]}" = "git-secret: cleaning up..." ]
[[ "${lines[1]}" == "git-secret: cleaning up:"* ]]

# New files should be created:
local encrypted_file=$(_get_encrypted_filename "$FILE_TO_HIDE")
Expand All @@ -166,7 +166,7 @@ function teardown {
# git secret hide -m, uses a temp file so cleaning should take place
[[ "${#lines[@]}" -eq 2 ]]
[ "${lines[0]}" = "git-secret: done. 1 of 1 files are hidden." ]
[ "${lines[1]}" = "git-secret: cleaning up..." ]
[[ "${lines[1]}" == "git-secret: cleaning up:"* ]]
# back path mappings
cp "${path_mappings}" "${path_mappings}.bak"
# run hide again
Expand Down
4 changes: 0 additions & 4 deletions tests/test_main.bats
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ function teardown {
# We will break things apart, so normally it won't run:
rm -r "./.git"

# This must fail:
run git secret usage
[ "$status" -eq 1 ]

# Dry run won't fail:
run git secret --dry-run
[ "$status" -eq 0 ]
Expand Down
4 changes: 3 additions & 1 deletion tests/test_usage.bats
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ function teardown {
}


@test "run 'usage' without '.git/'" {
@test "run 'usage' without '.git'" {
remove_git_repository

# It's ok for 'usage' to succeed when there's no .git directory, but it doesn't
run git secret usage
[ "$status" -eq 1 ]
}
Expand All @@ -36,6 +37,7 @@ function teardown {
#echo "# clear-line-output" >&3
#echo "# SECRETS_DIR is ${_SECRETS_DIR}" >&3

# It's ok for 'usage' to succeed when the .gitsecret directory is ignored, but it doesn't
run git secret usage
#echo "# git secret usage -> status $status" >&3

Expand Down
15 changes: 12 additions & 3 deletions utils/tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,25 @@

set -e

# Running all the bats-tests in a dir with spaces:
cd "${SECRET_PROJECT_ROOT}"; rm -rf 'tempdir with spaces'; mkdir 'tempdir with spaces'; cd 'tempdir with spaces';
#TEST_DIR="/tmp/tempdir with spaces"
TEST_DIR="/tmp/tempdir"

rm -rf "${TEST_DIR}"
mkdir "${TEST_DIR}"
chmod 0700 "${TEST_DIR}"
cd "${TEST_DIR}"

# test with non-standard SECRETS_DIR (normally .gitsecret) and SECRETS_EXTENSION (normally .secret)
export SECRETS_DIR=.gitsecret-testdir
export SECRETS_EXTENSION=.secret2
#export SECRETS_VERBOSE=''

export TMPDIR="${TEST_DIR}"
echo "# TMPDIR is $TMPDIR"

# bats expects diagnostic lines to be sent to fd 3, matching regex '^ #' (IE, like: `echo '# message here' >&3`)
# bats ... 3>&1 shows diagnostic output when errors occur.
bats "${SECRET_PROJECT_ROOT}/tests/" 3>&1

rm -rf 'tempdir with spaces'
cd ..; rm -rf "${TEST_DIR}"
trap 'echo "# git-secret: cleaning up ${TEST_DIR}"; rm -rf "${TEST_DIR}";' EXIT