Skip to content

Commit

Permalink
Add flag to change directory before running the command (#1914)
Browse files Browse the repository at this point in the history
Add -C/--change-directory flag to change the directory before running any
command. This allows to build, install or test packages without needing to
move to the directory with the source of the package, in a similar fashion
to similar flags other commands have.
  • Loading branch information
jsoriano authored Jun 18, 2024
1 parent 50b9c7f commit 5a9b108
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 97 deletions.
19 changes: 17 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package cmd

import (
"fmt"
"os"
"sort"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -52,7 +54,8 @@ func RootCmd() *cobra.Command {
)
},
}
rootCmd.PersistentFlags().BoolP(cobraext.VerboseFlagName, "v", false, cobraext.VerboseFlagDescription)
rootCmd.PersistentFlags().BoolP(cobraext.VerboseFlagName, cobraext.VerboseFlagShorthand, false, cobraext.VerboseFlagDescription)
rootCmd.PersistentFlags().StringP(cobraext.ChangeDirectoryFlagName, cobraext.ChangeDirectoryFlagShorthand, "", cobraext.ChangeDirectoryFlagDescription)

for _, cmd := range commands {
rootCmd.AddCommand(cmd.Command)
Expand All @@ -74,10 +77,22 @@ func processPersistentFlags(cmd *cobra.Command, args []string) error {
if err != nil {
return cobraext.FlagParsingError(err, cobraext.VerboseFlagName)
}

if verbose {
logger.EnableDebugMode()
}

changeDirectory, err := cmd.Flags().GetString(cobraext.ChangeDirectoryFlagName)
if err != nil {
return cobraext.FlagParsingError(err, cobraext.ChangeDirectoryFlagName)
}
if changeDirectory != "" {
err := os.Chdir(changeDirectory)
if err != nil {
return fmt.Errorf("failed to change directory: %w", err)
}
logger.Debugf("Running command in directory \"%s\"", changeDirectory)
}

return nil
}

Expand Down
5 changes: 5 additions & 0 deletions internal/cobraext/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ package cobraext
// Global flags
const (
VerboseFlagName = "verbose"
VerboseFlagShorthand = "v"
VerboseFlagDescription = "verbose mode"

ChangeDirectoryFlagName = "change-directory"
ChangeDirectoryFlagShorthand = "C"
ChangeDirectoryFlagDescription = "change to the specified directory before running the command"
)

// Primary flags reused by multiple commands
Expand Down
17 changes: 3 additions & 14 deletions scripts/test-build-zip.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ cleanup() {

# Clean used resources
for d in test/packages/*/*/; do
(
cd "$d"
elastic-package clean -v
)
elastic-package clean -C "$d" -v
done

exit $r
Expand All @@ -43,12 +40,8 @@ for d in test/packages/*/*/; do
if [ "$(testype $d)" == "false_positives" ]; then
continue
fi
(
cd $d
elastic-package build --zip --sign -v
)
elastic-package build -C "$d" --zip --sign -v
done
cd -

# Remove unzipped built packages, leave .zip files
rm -r build/packages/*/
Expand All @@ -62,9 +55,5 @@ for d in test/packages/*/*/; do
if [ "$(testype $d)" == "false_positives" ]; then
continue
fi
(
cd $d
elastic-package install -v
)
cd -
elastic-package install -C "$d" -v
done
22 changes: 5 additions & 17 deletions scripts/test-check-false-positives.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ function cleanup() {

# Clean used resources
for d in test/packages/${PACKAGE_TEST_TYPE:-false_positives}/${PACKAGE_UNDER_TEST:-*}/; do
(
cd "$d"
elastic-package clean -v
)
elastic-package clean -C "$d" -v
done

exit $r
Expand All @@ -36,10 +33,7 @@ function check_expected_errors() {
fi

rm -f ${result_tests}
(
cd "$package_root"
elastic-package test -v --report-format xUnit --report-output file --test-coverage --coverage-format=generic --defer-cleanup 1s || true
)
elastic-package test -C "$package_root" -v --report-format xUnit --report-output file --test-coverage --coverage-format=generic --defer-cleanup 1s || true

cat ${result_tests} | tr -d '\n' > ${results_no_spaces}

Expand Down Expand Up @@ -69,18 +63,12 @@ function check_build_output() {
local output_file="$PWD/build/elastic-package-output"

if [ ! -f "${expected_build_output}" ]; then
(
cd "$package_root"
elastic-package build -v
)
elastic-package build -C "$package_root" -v
return
fi

(
cd "$package_root"
mkdir -p "$(dirname "$output_file")"
elastic-package build 2>&1 | tee "$output_file" || true # Ignore errors here
)
mkdir -p "$(dirname "$output_file")"
elastic-package build -C "$package_root" 2>&1 | tee "$output_file" || true # Ignore errors here

diff -w -u "$expected_build_output" "$output_file" || (
echo "Error: Build output has differences with expected output"
Expand Down
88 changes: 38 additions & 50 deletions scripts/test-check-packages.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ cleanup() {

# Clean used resources
for d in test/packages/${PACKAGE_TEST_TYPE:-other}/${PACKAGE_UNDER_TEST:-*}/; do
(
cd "$d"
elastic-package clean -v
)
elastic-package clean -C "$d" -v
done

exit $r
Expand All @@ -51,15 +48,10 @@ ELASTIC_PACKAGE_LINKS_FILE_PATH="$(pwd)/scripts/links_table.yml"
export ELASTIC_PACKAGE_LINKS_FILE_PATH
export SERVERLESS=${SERVERLESS:-"false"}

OLDPWD=$PWD
# Build/check packages
for d in test/packages/${PACKAGE_TEST_TYPE:-other}/${PACKAGE_UNDER_TEST:-*}/; do
(
cd "$d"
elastic-package check -v
)
elastic-package check -C "$d" -v
done
cd -

if [ "${PACKAGE_TEST_TYPE:-other}" == "with-logstash" ]; then
# Create a logstash profile and use it
Expand Down Expand Up @@ -93,45 +85,41 @@ fi

# Run package tests
for d in test/packages/${PACKAGE_TEST_TYPE:-other}/${PACKAGE_UNDER_TEST:-*}/; do
(
cd "$d"
package_to_test=$(basename "${d}")

if [ "${PACKAGE_TEST_TYPE:-other}" == "benchmarks" ]; then
# It is not used PACKAGE_UNDER_TEST, so all benchmark packages are run in the same loop
if [ "${package_to_test}" == "pipeline_benchmark" ]; then
rm -rf "${OLDPWD}/build/benchmark-results"
elastic-package benchmark pipeline -v --report-format xUnit --report-output file --fail-on-missing

rm -rf "${OLDPWD}/build/benchmark-results-old"
mv "${OLDPWD}/build/benchmark-results" "${OLDPWD}/build/benchmark-results-old"

elastic-package benchmark pipeline -v --report-format json --report-output file --fail-on-missing

elastic-package report --fail-on-missing benchmark \
--new "${OLDPWD}/build/benchmark-results" \
--old "${OLDPWD}/build/benchmark-results-old" \
--threshold 1 --report-output-path="${OLDPWD}/build/benchreport"
fi
if [ "${package_to_test}" == "system_benchmark" ]; then
elastic-package benchmark system --benchmark logs-benchmark -v --defer-cleanup 1s
fi
elif [ "${PACKAGE_TEST_TYPE:-other}" == "with-logstash" ] && [ "${package_to_test}" == "system_benchmark" ]; then
elastic-package benchmark system --benchmark logs-benchmark -v --defer-cleanup 1s
else
if [[ "${SERVERLESS}" == "true" ]]; then
# skip system tests
elastic-package test asset -v --report-format xUnit --report-output file --defer-cleanup 1s --test-coverage --coverage-format=generic
elastic-package test static -v --report-format xUnit --report-output file --defer-cleanup 1s --test-coverage --coverage-format=generic
# FIXME: adding test-coverage for serverless results in errors like this:
# Error: error running package pipeline tests: could not complete test run: error calculating pipeline coverage: error fetching pipeline stats for code coverage calculations: need exactly one ES node in stats response (got 4)
elastic-package test pipeline -v --report-format xUnit --report-output file --defer-cleanup 1s
exit # as it is run in a subshell, it cannot be used "continue"
fi
# Run all tests
# defer-cleanup is set to a short period to verify that the option is available
elastic-package test -v --report-format xUnit --report-output file --defer-cleanup 1s --test-coverage --coverage-format=generic
package_to_test=$(basename "${d}")

if [ "${PACKAGE_TEST_TYPE:-other}" == "benchmarks" ]; then
# It is not used PACKAGE_UNDER_TEST, so all benchmark packages are run in the same loop
if [ "${package_to_test}" == "pipeline_benchmark" ]; then
rm -rf "${PWD}/build/benchmark-results"
elastic-package benchmark pipeline -C "$d" -v --report-format xUnit --report-output file --fail-on-missing

rm -rf "${PWD}/build/benchmark-results-old"
mv "${PWD}/build/benchmark-results" "${PWD}/build/benchmark-results-old"

elastic-package benchmark pipeline -C "$d" -v --report-format json --report-output file --fail-on-missing

elastic-package report -C "$d" --fail-on-missing benchmark \
--new "${PWD}/build/benchmark-results" \
--old "${PWD}/build/benchmark-results-old" \
--threshold 1 --report-output-path="${PWD}/build/benchreport"
fi
)
cd -
if [ "${package_to_test}" == "system_benchmark" ]; then
elastic-package benchmark system -C "$d" --benchmark logs-benchmark -v --defer-cleanup 1s
fi
elif [ "${PACKAGE_TEST_TYPE:-other}" == "with-logstash" ] && [ "${package_to_test}" == "system_benchmark" ]; then
elastic-package benchmark system -C "$d" --benchmark logs-benchmark -v --defer-cleanup 1s
else
if [[ "${SERVERLESS}" == "true" ]]; then
# skip system tests
elastic-package test asset -C "$d" -v --report-format xUnit --report-output file --defer-cleanup 1s --test-coverage --coverage-format=generic
elastic-package test static -C "$d" -v --report-format xUnit --report-output file --defer-cleanup 1s --test-coverage --coverage-format=generic
# FIXME: adding test-coverage for serverless results in errors like this:
# Error: error running package pipeline tests: could not complete test run: error calculating pipeline coverage: error fetching pipeline stats for code coverage calculations: need exactly one ES node in stats response (got 4)
elastic-package test pipeline -C "$d" -v --report-format xUnit --report-output file --defer-cleanup 1s
exit # as it is run in a subshell, it cannot be used "continue"
fi
# Run all tests
# defer-cleanup is set to a short period to verify that the option is available
elastic-package test -C "$d" -v --report-format xUnit --report-output file --defer-cleanup 1s --test-coverage --coverage-format=generic
fi
done
12 changes: 2 additions & 10 deletions scripts/test-install-zip.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ cleanup() {
elastic-package stack down -v

for d in test/packages/*/*/; do
(
cd "$d"
elastic-package clean -v
)
elastic-package clean -C "$d" -v
done

exit $r
Expand Down Expand Up @@ -98,20 +95,15 @@ elastic-package stack up -d -v ${ARG_VERSION}

ELASTIC_PACKAGE_LINKS_FILE_PATH="$(pwd)/scripts/links_table.yml"
export ELASTIC_PACKAGE_LINKS_FILE_PATH
OLDPWD=$PWD

# Build packages
for d in test/packages/*/*/; do
# Packages in false_positives can have issues.
if [ "$(testype "$d")" == "false_positives" ]; then
continue
fi
(
cd "$d"
elastic-package build
)
elastic-package build -C "$d"
done
cd "$OLDPWD"

# Remove unzipped built packages, leave .zip files
rm -r build/packages/*/
Expand Down
5 changes: 1 addition & 4 deletions scripts/test-system-test-flags.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ cleanup() {

# Clean used resources
for d in test/packages/*/*/; do
(
cd "$d"
elastic-package clean -v
)
elastic-package clean -C "$d" -v
done

exit $r
Expand Down

0 comments on commit 5a9b108

Please sign in to comment.