From 9f4e8e0314c801df65a742af93bb42ebaafc2a83 Mon Sep 17 00:00:00 2001 From: Panagiotis Simakis Date: Fri, 30 Dec 2022 17:54:47 +0200 Subject: [PATCH] Project structure refactoring (#30) * run tests on multiple os * refactor project structure * ignore shellcheck SC2034 * fix shellcheck * shellcheck follow files * shellcheck follow files * fix uninstall integration test * fix unittests * fix makefile * fix test setup * increase coverage * exclude macos from unit tests * rename dropboxignore.sh to cli.sh * fix snap version Co-authored-by: Panagiotis Simakis --- .github/workflows/shellcheck.yml | 10 +- .github/workflows/stats.yml | 2 +- .github/workflows/testing.yml | 24 +- Makefile | 9 +- README.md | 2 +- bin/dropboxignore.sh | 667 ---------------------------- docs/installation.md | 2 +- libs/bats-assert | 2 +- libs/bats-file | 2 +- libs/bats-support | 2 +- snap/snapcraft.yaml | 6 +- src/bin/cli.sh | 123 +++++ src/lib/commands/delete.sh | 32 ++ src/lib/commands/generate.sh | 28 ++ src/lib/commands/help.sh | 31 ++ src/lib/commands/ignore.sh | 45 ++ src/lib/commands/list.sh | 31 ++ src/lib/commands/revert.sh | 31 ++ src/lib/commands/update.sh | 19 + src/lib/commands/version.sh | 10 + src/lib/modules/base.sh | 19 + src/lib/modules/generate.sh | 26 ++ src/lib/modules/ignore.sh | 51 +++ src/lib/modules/loader.sh | 18 + src/lib/modules/logging.sh | 73 +++ src/lib/modules/revert.sh | 27 ++ src/lib/modules/update.sh | 23 + src/lib/modules/utils.sh | 135 ++++++ {utils => src/utils}/install.sh | 0 {utils => src/utils}/stats.sh | 0 tests/test_delete.bats | 40 ++ tests/test_generate.bats | 61 +++ tests/test_genupi.bats | 15 + tests/test_help.bats | 18 + tests/test_helper/common-setup.bash | 20 + tests/test_ignore.bats | 27 ++ tests/test_list.bats | 20 + tests/test_logging.bats | 56 +++ tests/test_revert.bats | 29 ++ tests/test_update.bats | 21 + tests/test_utils.bats | 88 ++++ tests/test_version.bats | 11 + tests/tests.bats | 336 -------------- 43 files changed, 1163 insertions(+), 1029 deletions(-) delete mode 100755 bin/dropboxignore.sh create mode 100755 src/bin/cli.sh create mode 100644 src/lib/commands/delete.sh create mode 100644 src/lib/commands/generate.sh create mode 100644 src/lib/commands/help.sh create mode 100644 src/lib/commands/ignore.sh create mode 100644 src/lib/commands/list.sh create mode 100644 src/lib/commands/revert.sh create mode 100644 src/lib/commands/update.sh create mode 100644 src/lib/commands/version.sh create mode 100644 src/lib/modules/base.sh create mode 100644 src/lib/modules/generate.sh create mode 100644 src/lib/modules/ignore.sh create mode 100644 src/lib/modules/loader.sh create mode 100644 src/lib/modules/logging.sh create mode 100644 src/lib/modules/revert.sh create mode 100644 src/lib/modules/update.sh create mode 100644 src/lib/modules/utils.sh rename {utils => src/utils}/install.sh (100%) rename {utils => src/utils}/stats.sh (100%) create mode 100644 tests/test_delete.bats create mode 100644 tests/test_generate.bats create mode 100644 tests/test_genupi.bats create mode 100644 tests/test_help.bats create mode 100644 tests/test_helper/common-setup.bash create mode 100644 tests/test_ignore.bats create mode 100644 tests/test_list.bats create mode 100644 tests/test_logging.bats create mode 100644 tests/test_revert.bats create mode 100644 tests/test_update.bats create mode 100644 tests/test_utils.bats create mode 100644 tests/test_version.bats delete mode 100755 tests/tests.bats diff --git a/.github/workflows/shellcheck.yml b/.github/workflows/shellcheck.yml index d0646a5..b7093d6 100644 --- a/.github/workflows/shellcheck.yml +++ b/.github/workflows/shellcheck.yml @@ -2,8 +2,14 @@ name: Shellcheck on: push: branches: [ master ] + paths: + - 'src/**' + - 'tests/**' pull_request: branches: [ master ] + paths: + - 'src/**' + - 'tests/**' jobs: shellcheck: @@ -13,5 +19,7 @@ jobs: - uses: actions/checkout@v2 - name: Run ShellCheck uses: ludeeus/action-shellcheck@master + env: + SHELLCHECK_OPTS: -x with: - ignore: docs icons libs snap venv .github + ignore_paths: docs icons libs snap venv .github diff --git a/.github/workflows/stats.yml b/.github/workflows/stats.yml index 55d63ce..00bc80c 100644 --- a/.github/workflows/stats.yml +++ b/.github/workflows/stats.yml @@ -18,4 +18,4 @@ jobs: run: sudo apt update && sudo apt install curl jq git - name: Create stats file - run: sh -c "$(curl -fsSL https://raw.githubusercontent.com/sp1thas/dropboxignore/master/utils/stats.sh)" '${{ secrets.GITHUB_TOKEN }}' + run: sh -c "$(curl -fsSL https://raw.githubusercontent.com/sp1thas/dropboxignore/master/src/utils/stats.sh)" '${{ secrets.GITHUB_TOKEN }}' diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml index 3b7813d..ccf5433 100644 --- a/.github/workflows/testing.yml +++ b/.github/workflows/testing.yml @@ -3,13 +3,14 @@ name: Testing on: push: branches: [ master ] - paths-ignore: - - '**.md' - - '**/mkdocs.yml' - - '**/shellcheck.yml' - - '**/stats.yml' + paths: + - 'src/**' + - 'tests/**' pull_request: branches: [ master ] + paths: + - 'src/**' + - 'tests/**' env: HOMEBREW_NO_AUTO_UPDATE: 1 @@ -20,7 +21,7 @@ jobs: strategy: fail-fast: false matrix: - os: [ ubuntu-20.04, ubuntu-22.04, macos-11, macos-12 ] + os: [ ubuntu-22.04 ] steps: - uses: actions/checkout@v3 - name: Install os dependencies @@ -44,19 +45,18 @@ jobs: run: make test - name: Coverage report run: | - kcov --include-path=bin/dropboxignore.sh /tmp/coverage bats tests/*.bats + kcov --include-path=src /tmp/coverage bats tests/*.bats cp $(find /tmp/coverage -type f -name 'cobertura.xml') coverage.xml - uses: codecov/codecov-action@v2 with: token: ${{ secrets.CODECOV_TOKEN }} fail_ci_if_error: true - integration: runs-on: ${{ matrix.os }} strategy: fail-fast: false matrix: - os: [ ubuntu-20.04, ubuntu-22.04, macos-11, macos-12 ] + os: [ ubuntu-22.04, macos-12 ] steps: - uses: actions/checkout@v3 - name: Install os dependencies @@ -68,12 +68,12 @@ jobs: fi - name: Install using wget run: | - sudo sh -c "$(curl -fsSL https://raw.githubusercontent.com/sp1thas/dropboxignore/master/utils/install.sh)" + sudo sh -c "$(curl -fsSL https://raw.githubusercontent.com/sp1thas/dropboxignore/master/src/utils/install.sh)" dropboxignore version sudo rm /usr/local/bin/dropboxignore - name: Install using curl run: | - sudo sh -c "$(curl -fsSL https://raw.githubusercontent.com/sp1thas/dropboxignore/master/utils/install.sh)" + sudo sh -c "$(curl -fsSL https://raw.githubusercontent.com/sp1thas/dropboxignore/master/src/utils/install.sh)" dropboxignore version sudo rm /usr/local/bin/dropboxignore - name: Install from source @@ -81,4 +81,4 @@ jobs: sudo make install dropboxignore version - name: Uninstall - run: make uninstall + run: sudo make uninstall diff --git a/Makefile b/Makefile index 4a875bd..d60f979 100644 --- a/Makefile +++ b/Makefile @@ -7,13 +7,16 @@ test : # install dropboxignore install : - cp bin/dropboxignore.sh ${DESTDIR}/bin/dropboxignore + cp src/bin/cli.sh ${DESTDIR}/bin/dropboxignore chmod +x ${DESTDIR}/bin/dropboxignore - dropboxignore version + mkdir -p ${DESTDIR}/lib/dropboxignore + cp -r src/lib/commands ${DESTDIR}/lib/dropboxignore/. + cp -r src/lib/modules ${DESTDIR}/lib/dropboxignore/. + echo "\e[32mdropboxignore has been installed!" # uninstall dropboxignore uninstall : - rm -rf "${DESTDIR}/bin/dropboxignore" + rm -rf "${DESTDIR}/bin/dropboxignore" "${DESTDIR}/lib/dropboxignore" echo "\e[32mdropboxignore has been uninstalled." # create snap diff --git a/README.md b/README.md index 0288640..53ad6d7 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ dropboxignore is installed by running one of the following commands in your term | curl | sudo sh -c "$(curl -fsSL https://rb.gy/12plgs)" c | | wget | sudo sh -c "$(wget -qO- https://rb.gy/12plgs)" w | -Worried about mysterious shorted urls like? Take a look at the installation script here: [`https://rb.gy/12plgs --> https://raw.githubusercontent.com/sp1thas/dropboxignore/master/utils/install.sh`](https://raw.githubusercontent.com/sp1thas/dropboxignore/master/utils/install.sh)) +Worried about mysterious shorted urls like? Take a look at the installation script here: [`https://rb.gy/12plgs --> https://raw.githubusercontent.com/sp1thas/dropboxignore/master/src/utils/install.sh`](https://raw.githubusercontent.com/sp1thas/dropboxignore/master/utils/install.sh)) ### Snap [![Get it from the Snap Store](https://snapcraft.io/static/images/badges/en/snap-store-white.svg)](https://snapcraft.io/dropboxignore) diff --git a/bin/dropboxignore.sh b/bin/dropboxignore.sh deleted file mode 100755 index 45363b5..0000000 --- a/bin/dropboxignore.sh +++ /dev/null @@ -1,667 +0,0 @@ -#!/bin/bash -# dropboxignore -# ============= -# Ignore files and folders from dropbox using the .dropbox ignore files - -IFS=' -' -set -f -VERSION=v1.1.1 -DROPBOX_IGNORE_FILE_NAME=".dropboxignore" -GIT_IGNORE_FILE_NAME=".gitignore" -MACHINE="$(uname -s)" -PROGRAM_NAME="$(basename "$0")" -VERBOSITY=1 -TOTAL_N_IGNORED_FILES=0 -TOTAL_N_IGNORED_FOLDERS=0 -TOTAL_N_REVERTED_FILES=0 -TOTAL_N_GENERATED_FILES=0 -BASE_FOLDER="$PWD" -FILE_ATTR_NAME="com.dropbox.ignored" -[[ $MACHINE == Darwin ]] && GREP_CMD="ggrep" || GREP_CMD="grep" -[[ $MACHINE == Darwin ]] && DIFF_CMD="$(brew --prefix)/bin/diff" || DIFF_CMD="diff" - -DEFAULT="\e[0m" -GREEN="\e[32m" -BLUE="\e[34m" -RED="\e[31m" -YELLOW="\e[33m" - -####################################### -# Log info message. -# Globals: -# VERBOSITY -# Arguments: -# Info Message. -# Outputs: -# Info Message -####################################### -log_info() { - if [ "$VERBOSITY" -ge 1 ]; then - echo -e "$(date) $GREEN [ INFO ] $1 $DEFAULT" - fi -} - -####################################### -# Log debug message. -# Globals: -# VERBOSITY -# Arguments: -# Debug Message. -# Outputs: -# Debug Message -####################################### -log_debug() { - if [ "$VERBOSITY" -ge 2 ]; then - echo -e "$(date) $BLUE [ DEBUG ] $1 $DEFAULT" - fi -} - -####################################### -# Log error message. -# Globals: -# VERBOSITY -# Arguments: -# Error Message. -# Exit status. -# Outputs: -# Error message. -####################################### -log_error() { - if [ "$VERBOSITY" -ge 0 ]; then - echo -e "$(date) $RED [ ERROR ] $1 $DEFAULT" - fi - if [ -z "$2" ]; then - exit 1 - else - exit "$2" - fi -} - -####################################### -# Log warning message. -# Globals: -# VERBOSITY -# Arguments: -# Warning message. -# Outputs: -# Warning message. -####################################### -log_warning() { - if [ "$VERBOSITY" -ge 1 ]; then - echo -e "$(date) $YELLOW [WARNING] $1 $DEFAULT" - fi -} - -####################################### -# Check OS. -# Globals: -# MACHINE -####################################### -check_os() { - case $MACHINE in - Linux) ;; - - Darwin) ;; - - *) - log_error "$MACHINE is not supported" 3 - ;; - esac -} - -####################################### -# Check system dependencies. -# Globals: -# MACHINE -####################################### -check_dependencies() { - if command -v realpath &>/dev/null; then - log_debug "realpath command is installed" - elif command -v python &>/dev/null; then - log_debug "python is installed" - else - log_error "Neither realpath command not python could be found in you system" - fi - case $MACHINE in - Linux) - if ! command -v getfattr &>/dev/null; then - log_error "attr package is not installed" 5 - fi - if ! command -v attr &>/dev/null; then - log_error "attr package is not installed" 5 - fi - log_debug "attr package is installed" - - ;; - Darwin) - if ! command -v xattr &>/dev/null; then - log_error "xattr package not installed" 5 - fi - ;; - *) - log_error "$MACHINE is not supported" 3 - ;; - esac -} - -check_os - -####################################### -# Check input file or folder. -# Globals: -# DROPBOX_IGNORE_FILE_NAME -# BASE_FOLDER -# Arguments: -# Input file or folder. -# Outputs: -# Input file/folder or error if not exists. -# Returns: -# 0 if file or folder exists, otherwise, returns 2. -####################################### -check_input() { - if [ -z "$1" ]; then - log_error "You have to provide a file or folder" 2 - elif [ -d "$1" ]; then - log_debug "Input folder: \"$1\"" - BASE_FOLDER="$1" - elif [ -f "$1" ] || { [ "$(basename "$1")" == "$DROPBOX_IGNORE_FILE_NAME" ] && [ -d "$(dirname "$1")" ]; }; then - log_debug "Input file: \"$1\"" - BASE_FOLDER="$(dirname "$1")" - else - log_error "\"$1\" does not exists" 2 - fi -} - -####################################### -# Find all .gitignore files. -# Globals: -# GIT_IGNORE_FILE_NAME -# Arguments: -# Input file or folder. -####################################### -find_gitignore_files() { - if [ -d "$1" ]; then - GITIGNORE_FILES=$(find "$1" -type f -name "$GIT_IGNORE_FILE_NAME") - else - GITIGNORE_FILES=$(find "$(dirname "$1")" -maxdepth 1 -type f -name "$GIT_IGNORE_FILE_NAME") - fi -} - -####################################### -# Get relative path from absolute path. -# Arguments: -# Absolute path. -# Base Absolute path. -####################################### -get_relative_path() { - if command -v realpath &>/dev/null; then - realpath --relative-to="${2-$PWD}" "$1" - else - python -c 'import os.path, sys;print(os.path.relpath(sys.argv[1],sys.argv[2]))' "$1" "${2-$PWD}" - fi -} - -####################################### -# Get absolute path from relative path. -# Arguments: -# Relative path. -####################################### -get_absolute_path() { - if command -v realpath &>/dev/null; then - realpath "$1" - else - python -c 'import os.path, sys;print(os.path.abspath(sys.argv[1]))' "$1" - fi -} - -####################################### -# Delete all .dropboxignore files -# Globals: -# DROPBOX_IGNORE_FILE_NAME -# Arguments: -# Input folder. -# Outputs: -# Warning message if file not found. -# Returns: -# 3 if file not found, otherwise, 0. -####################################### -delete_dropboxignore_files() { - if [ -d "$1" ]; then - n_results=0 - while read -r file_path; do - ((n_results++)) - rm "$file_path" - done < <(find "$1" -type f -name "$DROPBOX_IGNORE_FILE_NAME") - echo -e "$YELLOW Deleted files: $n_results $DEFAULT" - elif [ -f "$1" ]; then - if [ "$(basename "$1")" == "$DROPBOX_IGNORE_FILE_NAME" ]; then - rm "$1" - echo -e "$YELLOW Removed file: $(get_relative_path "$1" "$BASE_FOLDER") $DEFAULT" - else - log_error "Given file is not a $DROPBOX_IGNORE_FILE_NAME file." - fi - else - log_error "file not found" 3 - fi -} - -####################################### -# Find all .dropboxignore files. -# Globals: -# DROPBOX_IGNORE_FILE_NAME -# DROPBOX_IGNORE_FILES -# Arguments: -# Input folder or file. -####################################### -find_dropboxignore_files() { - DROPBOX_IGNORE_FILES=$(find "$1" -type f -name "$DROPBOX_IGNORE_FILE_NAME") -} - -####################################### -# Generate a .dropboxignore file based on .gitignore file. -# Globals: -# DROPBOX_IGNORE_FILE_NAME -# Arguments: -# .dropboxignore file path (even if not exists). -# Outputs: -# Status about the generated file. -####################################### -generate_dropboxignore_file() { - dropboxignore_file_path="$(dirname "$1")/$DROPBOX_IGNORE_FILE_NAME" - if [ -f "$dropboxignore_file_path" ]; then - log_debug "Already existing file: $(get_relative_path "$dropboxignore_file_path" "$BASE_FOLDER")" - else - tee "$dropboxignore_file_path" >/dev/null </dev/null </dev/null)" - ;; - esac -} - -####################################### -# Ignore file. -# Globals: -# DROPBOX_IGNORE_FILE_NAME -# BASE_FOLDER -# Arguments: -# Input file. -####################################### -ignore_file() { - file_ignore_status "$1" - if [ -z "$FILE_ATTR_VALUE" ]; then - if [ -f "$1" ]; then - ((TOTAL_N_IGNORED_FILES++)) - else - ((TOTAL_N_IGNORED_FOLDERS++)) - fi - case $MACHINE in - Linux) - attr -s "$FILE_ATTR_NAME" -V 1 "$1" >/dev/null - ;; - Darwin) - xattr -w "$FILE_ATTR_NAME" 1 "$1" >/dev/null - ;; - esac - log_debug "Ignored file: $(get_relative_path "$1" "$BASE_FOLDER")" - else - log_debug "Already ignored file: $(get_relative_path "$1" "$BASE_FOLDER")" - fi -} - -####################################### -# Mark matched files as ignored. -# Globals: -# DROPBOX_IGNORE_FILES -# Arguments: -# Input folder. -# Outputs: -# Number of ignored files. -####################################### -ignore_files() { - if [ "$(basename "$1")" == "$DROPBOX_IGNORE_FILE_NAME" ]; then - log_error "Cannot ignore an $DROPBOX_IGNORE_FILE_NAME. Choose another file or folder." 4 - elif [ -f "$1" ]; then - ignore_file "$1" - elif [ -d "$1" ]; then - find_dropboxignore_files "$1" - if [ -z "$DROPBOX_IGNORE_FILES" ]; then - ignore_file "$1" - else - for dropboxignore_file in $DROPBOX_IGNORE_FILES; do - file_total_results=0 - # shellcheck disable=SC2013 - if "$GREP_CMD" -q -P '^\s*!' "$dropboxignore_file"; then - echo -e "$YELLOW$(get_relative_path "$dropboxignore_file" "$BASE_FOLDER") contains exception patterns, will be ignored" - continue - fi - while read -r file_pattern; do - file_pattern=${file_pattern%/} - subdir="$(dirname "$file_pattern")" - pattern="$(basename "$file_pattern")" - n_results=0 - while read -r file_path; do - ignore_file "$file_path" - ((n_results++)) - done < <(find "$(dirname "$dropboxignore_file")/$subdir" -name "$pattern") - file_total_results=$((file_total_results + n_results)) - done < <("$GREP_CMD" -v -P '^\s*$|^\s*\#|^\s*!' "$dropboxignore_file") - log_debug "Matched files because of '$(get_relative_path "$dropboxignore_file" "$BASE_FOLDER")': $file_total_results" - done - fi - fi - echo -e "${BLUE}Total number of ignored files: $TOTAL_N_IGNORED_FILES $DEFAULT\nTotal number of ignored folders: $TOTAL_N_IGNORED_FOLDERS $DEFAULT" -} - -####################################### -# List ignored files and folders. -# Arguments: -# Input folder. -# Filtering pattern -# Outputs: -# Ignored files and folders -####################################### -list_ignored() { - total_ignored_files=0 - total_ignored_folders=0 - if [ -z "$2" ]; then - filtering_pattern='*' - else - filtering_pattern="$2" - fi - while read -r f_path; do - file_ignore_status "$f_path" - if [ -n "$FILE_ATTR_VALUE" ]; then - get_relative_path "$f_path" "$BASE_FOLDER" - if [ -f "$f_path" ]; then - ((total_ignored_files++)) - elif [ -d "$f_path" ]; then - ((total_ignored_folders++)) - fi - fi - done < <(find "$1" -name "$filtering_pattern") - echo -e "${YELLOW}Total number of ignored files: $total_ignored_files\nTotal number of ignored folders: $total_ignored_folders $DEFAULT" -} - -####################################### -# Revert ignored file. -# Arguments: -# Input file. -# Outputs: -# Message about the reverted file. -####################################### -revert_ignored() { - file_ignore_status "$1" - if [ "$FILE_ATTR_VALUE" == 1 ]; then - case $MACHINE in - Linux) - attr -r "$FILE_ATTR_NAME" "$1" >/dev/null - ;; - Darwin) - xattr -d "$FILE_ATTR_NAME" "$1" >/dev/null - ;; - esac - - log_debug "Reverted file: $(get_relative_path "$1" "$BASE_FOLDER")" - ((TOTAL_N_REVERTED_FILES++)) - else - log_debug "Already reverted file: $(get_relative_path "$1" "$BASE_FOLDER")" - fi -} - -####################################### -# Revert all ignored files. -# Arguments: -# Input folder. -# Outputs: -# Message about reverted files. -####################################### -revert_ignored_files() { - if [ -f "$1" ]; then - revert_ignored "$1" - else - while read -r file_path; do - file_ignore_status "$file_path" - if [ "$FILE_ATTR_VALUE" == 1 ]; then - revert_ignored "$file_path" - fi - done < <(find "$1" -type f) - echo -e "${BLUE}Total number of reverted files: $TOTAL_N_REVERTED_FILES $DEFAULT" - TOTAL_N_REVERTED_FILES=0 - while read -r file_path; do - file_ignore_status "$file_path" - if [ "$FILE_ATTR_VALUE" == 1 ]; then - revert_ignored "$file_path" - ((n_results++)) - fi - done < <(find "$1" -type d) - echo -e "${BLUE}Total number of reverted folders: $TOTAL_N_REVERTED_FILES $DEFAULT" - fi -} - -####################################### -# Print help message. -# Outputs: -# help message -####################################### -display_help() { - cat < [-v 0-2] [-p pattern] - - Commands: - - genupi Generate, update & ignore using one shortcut command. - generate Generate .dropboxignore files based on existing .gitignore files. - update Update existing .dropboxignore files if at least one .gitignore file has been changed. - ignore Ignore file or folder from dropbox under the given path. - list List ignored files and folders under the given path. - revert Revert ignored file or folder under the given path. - delete Delete specific .dropboxignore file or every .dropboxignore file under the given path. - help Will print this message and then will exit. - version Will print the version and then will exit. - - Options: - -v Choose verbose level (0: Error, 1: Info, 2: Debug) - -p Filtering pattern (Can be used with list command in order to filter results) - -EOUSAGE -} - -####################################### -# The main function. -# Arguments: -# Command -# Path -# Extra parameters -# Globals: -# PROGRAM_NAME -# VERBOSITY -# FILTERING_PATTERN -# MACHINE -# Output: -# Almost everything. -####################################### -main() { - if [ $# -eq 0 ]; then - display_help - return - fi - case $1 in - genupi) - genupi_action=true - shift - ;; - generate) - generate_action=true - shift - ;; - update) - update_action=true - shift - ;; - ignore) - ignore_action=true - shift - ;; - revert) - revert_action=true - shift - ;; - delete) - delete_action=true - shift - ;; - help) - display_help - exit 0 - ;; - version) - echo "$PROGRAM_NAME: $VERSION" - exit 0 - ;; - list) - list_action=true - shift - ;; - *) - log_error "$PROGRAM_NAME: '$1' is not a $PROGRAM_NAME command.\nSee '$PROGRAM_NAME help'" 1 - ;; - esac - - input_f="$1" - - shift - - while getopts ':pv:' opt; do - case "$opt" in - v) - VERBOSITY=$OPTARG - ;; - p) - FILTERING_PATTERN=$OPTARG - ;; - \?) - log_error "Unknown option: -$OPTARG" 3 - ;; - esac - done - - log_debug "Operating system: $MACHINE" - check_dependencies - - # check input file or folder - check_input "$input_f" - input_f=$(get_absolute_path "$input_f") - - # run action - if [ "$genupi_action" == true ]; then - generate_dropboxignore_files "$input_f" - update_dropboxignore_files "$input_f" - ignore_files "$input_f" - elif [ "$generate_action" == true ]; then - generate_dropboxignore_files "$input_f" - elif [ "$update_action" == true ]; then - update_dropboxignore_files "$input_f" - elif [ "$ignore_action" == true ]; then - ignore_files "$input_f" - elif [ "$delete_action" == true ]; then - delete_dropboxignore_files "$input_f" - elif [ "$revert_action" == true ]; then - revert_ignored_files "$input_f" - elif [ "$list_action" == true ]; then - list_ignored "$input_f" "$FILTERING_PATTERN" - fi -} - -main "$@" diff --git a/docs/installation.md b/docs/installation.md index 724209b..b012ed2 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -15,7 +15,7 @@ Linux systems. For macOS, Homebrew is required. sudo sh -c "$(wget -qO- https://rb.gy/12plgs)" w ``` -Worried about mysterious shorted urls like? Take a look at the installation script here: [`https://rb.gy/12plgs --> https://raw.githubusercontent.com/sp1thas/dropboxignore/master/utils/install.sh`](https://raw.githubusercontent.com/sp1thas/dropboxignore/master/utils/install.sh)) +Worried about mysterious shorted urls like? Take a look at the installation script here: [`https://rb.gy/12plgs --> https://raw.githubusercontent.com/sp1thas/dropboxignore/master/src/utils/install.sh`](https://raw.githubusercontent.com/sp1thas/dropboxignore/master/utils/install.sh)) ## Snap diff --git a/libs/bats-assert b/libs/bats-assert index 4bdd58d..78fa631 160000 --- a/libs/bats-assert +++ b/libs/bats-assert @@ -1 +1 @@ -Subproject commit 4bdd58d3fbcdce3209033d44d884e87add1d8405 +Subproject commit 78fa631d1370562d2cd4a1390989e706158e7bf0 diff --git a/libs/bats-file b/libs/bats-file index 17fa557..f9154f4 160000 --- a/libs/bats-file +++ b/libs/bats-file @@ -1 +1 @@ -Subproject commit 17fa557f6fe28a327933e3fa32efef1d211caa5a +Subproject commit f9154f43104322f9a30fb0a9e63c7cc46c5a8dc2 diff --git a/libs/bats-support b/libs/bats-support index d140a65..3c8fadc 160000 --- a/libs/bats-support +++ b/libs/bats-support @@ -1 +1 @@ -Subproject commit d140a65044b2d6810381935ae7f0c94c7023c8c3 +Subproject commit 3c8fadc5097c9acfc96d836dced2bb598e48b009 diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index f862383..5c3a138 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -15,7 +15,7 @@ architectures: apps: dropboxignore: - command: ./bin/dropboxignore.sh + command: ./src/bin/cli.sh plugs: - home @@ -27,7 +27,7 @@ parts: plugin: nil override-pull: | snapcraftctl pull - snapcraftctl set-version "$(curl -sSL https://raw.githubusercontent.com/sp1thas/dropboxignore/master/bin/dropboxignore.sh | grep -oP '^VERSION=(.*)$' | sed -r 's/^VERSION=//')" + snapcraftctl set-version "$(curl -sSL https://raw.githubusercontent.com/sp1thas/dropboxignore/master/src/lib/modules/base.sh | grep -oP '^VERSION=(.*)$' | sed -r 's/^VERSION=//')" main-part: stage-packages: @@ -35,6 +35,4 @@ parts: source-type: git source: https://github.com/sp1thas/dropboxignore.git plugin: dump - organize: - dropboxignore.sh: dropboxignore diff --git a/src/bin/cli.sh b/src/bin/cli.sh new file mode 100755 index 0000000..3fde3ea --- /dev/null +++ b/src/bin/cli.sh @@ -0,0 +1,123 @@ +#!/bin/bash +# dropboxignore +# ============= +# Ignore files and folders from dropbox using the .dropbox ignore files + +SCRIPT_PATH="$(cd -- "$(dirname "$0")" >/dev/null 2>&1 && pwd -P )" + +if [ "$SCRIPT_PATH" == "/usr/local/bin" ]; then + LIB_PATH="/usr/local/lib/dropboxignore" +else + LIB_PATH="$SCRIPT_PATH/../lib" +fi +# shellcheck disable=SC1091 +source "$LIB_PATH/modules/loader.sh" "$LIB_PATH" + +####################################### +# The main function. +# Arguments: +# Command +# Path +# Extra parameters +# Globals: +# PROGRAM_NAME +# VERBOSITY +# FILTERING_PATTERN +# MACHINE +# Output: +# Almost everything. +####################################### +main() { + if [ $# -eq 0 ]; then + cmd_help + return + fi + case $1 in + genupi) + genupi_action=true + shift + ;; + generate) + generate_action=true + shift + ;; + update) + update_action=true + shift + ;; + ignore) + ignore_action=true + shift + ;; + revert) + revert_action=true + shift + ;; + delete) + delete_action=true + shift + ;; + help) + cmd_help + exit 0 + ;; + version) + cmd_version + exit 0 + ;; + list) + list_action=true + shift + ;; + *) + log_error "$PROGRAM_NAME: '$1' is not a $PROGRAM_NAME command.\nSee '$PROGRAM_NAME help'" 1 + ;; + esac + + input_f="$1" + + shift + + while getopts ':pv:' opt; do + case "$opt" in + v) + # shellcheck disable=SC2034 + VERBOSITY=$OPTARG + ;; + p) + FILTERING_PATTERN=$OPTARG + ;; + \?) + log_error "Unknown option: -$OPTARG" 3 + ;; + esac + done + + log_debug "Operating system: $MACHINE" + check_dependencies + + # check input file or folder + check_input "$input_f" + input_f=$(get_absolute_path "$input_f") + + # run action + if [ "$genupi_action" == true ]; then + cmd_generate "$input_f" + cmd_update "$input_f" + cmd_ignore "$input_f" + elif [ "$generate_action" == true ]; then + cmd_generate "$input_f" + elif [ "$update_action" == true ]; then + cmd_update "$input_f" + elif [ "$ignore_action" == true ]; then + cmd_ignore "$input_f" + elif [ "$delete_action" == true ]; then + cmd_delete "$input_f" + elif [ "$revert_action" == true ]; then + cmd_revert "$input_f" + elif [ "$list_action" == true ]; then + cmd_list "$input_f" "$FILTERING_PATTERN" + fi +} + +main "$@" diff --git a/src/lib/commands/delete.sh b/src/lib/commands/delete.sh new file mode 100644 index 0000000..04d7d9a --- /dev/null +++ b/src/lib/commands/delete.sh @@ -0,0 +1,32 @@ +#!/bin/bash + +####################################### +# Delete all .dropboxignore files +# Globals: +# DROPBOX_IGNORE_FILE_NAME +# Arguments: +# Input folder. +# Outputs: +# Warning message if file not found. +# Returns: +# 3 if file not found, otherwise, 0. +####################################### +cmd_delete() { + if [ -d "$1" ]; then + n_results=0 + while read -r file_path; do + ((n_results++)) + rm "$file_path" + done < <(find "$1" -type f -name "$DROPBOX_IGNORE_FILE_NAME") + echo -e "$YELLOW Deleted files: $n_results $DEFAULT" + elif [ -f "$1" ]; then + if [ "$(basename "$1")" == "$DROPBOX_IGNORE_FILE_NAME" ]; then + rm "$1" + echo -e "$YELLOW Removed file: $(get_relative_path "$1" "$BASE_FOLDER") $DEFAULT" + else + log_error "Given file is not a $DROPBOX_IGNORE_FILE_NAME file." + fi + else + log_error "file not found" 3 + fi +} \ No newline at end of file diff --git a/src/lib/commands/generate.sh b/src/lib/commands/generate.sh new file mode 100644 index 0000000..e94066f --- /dev/null +++ b/src/lib/commands/generate.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +####################################### +# Generate all .dropboxignore files. +# Globals: +# DROPBOX_IGNORE_FILE_NAME +# GITIGNORE_FILES +# Arguments: +# Input folder. +####################################### +cmd_generate() { + find_gitignore_files "$1" + for gitignore_file in $GITIGNORE_FILES; do + if "$GREP_CMD" -q -P '^\s*!' "$gitignore_file"; then + echo -e "$YELLOW$(get_relative_path "$gitignore_file" "$BASE_FOLDER") contains exception patterns, will be ignored" + continue + fi + current_dir="$(dirname "$gitignore_file")" + dropboxignore_file="$current_dir/$DROPBOX_IGNORE_FILE_NAME" + if [ -f "$dropboxignore_file" ]; then + log_debug "Already existing file: $(get_relative_path "$dropboxignore_file" "$BASE_FOLDER")" + else + generate_dropboxignore_file "$gitignore_file" + ((TOTAL_N_GENERATED_FILES++)) + fi + done + echo -e "${YELLOW}Total number of generated files: $TOTAL_N_GENERATED_FILES $DEFAULT" +} \ No newline at end of file diff --git a/src/lib/commands/help.sh b/src/lib/commands/help.sh new file mode 100644 index 0000000..ae2d744 --- /dev/null +++ b/src/lib/commands/help.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +####################################### +# Print help message. +# Outputs: +# help message +####################################### +cmd_help() { + cat < [-v 0-2] [-p pattern] + + Commands: + + genupi Generate, update & ignore using one shortcut command. + generate Generate .dropboxignore files based on existing .gitignore files. + update Update existing .dropboxignore files if at least one .gitignore file has been changed. + ignore Ignore file or folder from dropbox under the given path. + list List ignored files and folders under the given path. + revert Revert ignored file or folder under the given path. + delete Delete specific .dropboxignore file or every .dropboxignore file under the given path. + help Will print this message and then will exit. + version Will print the version and then will exit. + + Options: + -v Choose verbose level (0: Error, 1: Info, 2: Debug) + -p Filtering pattern (Can be used with list command in order to filter results) + + + +EOUSAGE +} \ No newline at end of file diff --git a/src/lib/commands/ignore.sh b/src/lib/commands/ignore.sh new file mode 100644 index 0000000..ab60781 --- /dev/null +++ b/src/lib/commands/ignore.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +####################################### +# Mark matched files as ignored. +# Globals: +# DROPBOX_IGNORE_FILES +# Arguments: +# Input folder. +# Outputs: +# Number of ignored files. +####################################### +cmd_ignore() { + if [ "$(basename "$1")" == "$DROPBOX_IGNORE_FILE_NAME" ]; then + log_error "Cannot ignore an $DROPBOX_IGNORE_FILE_NAME. Choose another file or folder." 4 + elif [ -f "$1" ]; then + ignore_file "$1" + elif [ -d "$1" ]; then + find_dropboxignore_files "$1" + if [ -z "$DROPBOX_IGNORE_FILES" ]; then + ignore_file "$1" + else + for dropboxignore_file in $DROPBOX_IGNORE_FILES; do + file_total_results=0 + # shellcheck disable=SC2013 + if "$GREP_CMD" -q -P '^\s*!' "$dropboxignore_file"; then + echo -e "$YELLOW$(get_relative_path "$dropboxignore_file" "$BASE_FOLDER") contains exception patterns, will be ignored" + continue + fi + while read -r file_pattern; do + file_pattern=${file_pattern%/} + subdir="$(dirname "$file_pattern")" + pattern="$(basename "$file_pattern")" + n_results=0 + while read -r file_path; do + ignore_file "$file_path" + ((n_results++)) + done < <(find "$(dirname "$dropboxignore_file")/$subdir" -name "$pattern") + file_total_results=$((file_total_results + n_results)) + done < <("$GREP_CMD" -v -P '^\s*$|^\s*\#|^\s*!' "$dropboxignore_file") + log_debug "Matched files because of '$(get_relative_path "$dropboxignore_file" "$BASE_FOLDER")': $file_total_results" + done + fi + fi + echo -e "${BLUE}Total number of ignored files: $TOTAL_N_IGNORED_FILES $DEFAULT\nTotal number of ignored folders: $TOTAL_N_IGNORED_FOLDERS $DEFAULT" +} \ No newline at end of file diff --git a/src/lib/commands/list.sh b/src/lib/commands/list.sh new file mode 100644 index 0000000..b60bcb5 --- /dev/null +++ b/src/lib/commands/list.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +####################################### +# List ignored files and folders. +# Arguments: +# Input folder. +# Filtering pattern +# Outputs: +# Ignored files and folders +####################################### +cmd_list() { + total_ignored_files=0 + total_ignored_folders=0 + if [ -z "$2" ]; then + filtering_pattern='*' + else + filtering_pattern="$2" + fi + while read -r f_path; do + file_ignore_status "$f_path" + if [ -n "$FILE_ATTR_VALUE" ]; then + get_relative_path "$f_path" "$BASE_FOLDER" + if [ -f "$f_path" ]; then + ((total_ignored_files++)) + elif [ -d "$f_path" ]; then + ((total_ignored_folders++)) + fi + fi + done < <(find "$1" -name "$filtering_pattern") + echo -e "${YELLOW}Total number of ignored files: $total_ignored_files\nTotal number of ignored folders: $total_ignored_folders $DEFAULT" +} diff --git a/src/lib/commands/revert.sh b/src/lib/commands/revert.sh new file mode 100644 index 0000000..86da662 --- /dev/null +++ b/src/lib/commands/revert.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +####################################### +# Revert all ignored files. +# Arguments: +# Input folder. +# Outputs: +# Message about reverted files. +####################################### +cmd_revert() { + if [ -f "$1" ]; then + revert_ignored "$1" + else + while read -r file_path; do + file_ignore_status "$file_path" + if [ "$FILE_ATTR_VALUE" == 1 ]; then + revert_ignored "$file_path" + fi + done < <(find "$1" -type f) + echo -e "${BLUE}Total number of reverted files: $TOTAL_N_REVERTED_FILES $DEFAULT" + TOTAL_N_REVERTED_FILES=0 + while read -r file_path; do + file_ignore_status "$file_path" + if [ "$FILE_ATTR_VALUE" == 1 ]; then + revert_ignored "$file_path" + ((n_results++)) + fi + done < <(find "$1" -type d) + echo -e "${BLUE}Total number of reverted folders: $TOTAL_N_REVERTED_FILES $DEFAULT" + fi +} \ No newline at end of file diff --git a/src/lib/commands/update.sh b/src/lib/commands/update.sh new file mode 100644 index 0000000..1fc1961 --- /dev/null +++ b/src/lib/commands/update.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +####################################### +# Update all .dropboxignore files. +# Globals: +# GITIGNORE_FILES +# DROPBOX_IGNORE_FILE_NAME +# Arguments: +# Input folder. +####################################### +cmd_update() { + find_gitignore_files "$1" + for gitignore_file in $GITIGNORE_FILES; do + dropboxignore_file="$(dirname "$gitignore_file")/$DROPBOX_IGNORE_FILE_NAME" + if [ -f "$dropboxignore_file" ]; then + update_dropboxignore_file "$gitignore_file" "$dropboxignore_file" + fi + done +} \ No newline at end of file diff --git a/src/lib/commands/version.sh b/src/lib/commands/version.sh new file mode 100644 index 0000000..54b7105 --- /dev/null +++ b/src/lib/commands/version.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +####################################### +# Print version. +# Outputs: +# version +####################################### +cmd_version() { + echo "$PROGRAM_NAME: $VERSION" +} \ No newline at end of file diff --git a/src/lib/modules/base.sh b/src/lib/modules/base.sh new file mode 100644 index 0000000..3ac4f05 --- /dev/null +++ b/src/lib/modules/base.sh @@ -0,0 +1,19 @@ +#!/bin/bash +# shellcheck disable=SC2034 +IFS=' +' +set -f +VERSION=v1.2.1 +DROPBOX_IGNORE_FILE_NAME=".dropboxignore" +GIT_IGNORE_FILE_NAME=".gitignore" +MACHINE="$(uname -s)" +PROGRAM_NAME="dropboxignore" +VERBOSITY=1 +TOTAL_N_IGNORED_FILES=0 +TOTAL_N_IGNORED_FOLDERS=0 +TOTAL_N_REVERTED_FILES=0 +TOTAL_N_GENERATED_FILES=0 +BASE_FOLDER="$PWD" +FILE_ATTR_NAME="com.dropbox.ignored" +[[ $MACHINE == Darwin ]] && GREP_CMD="ggrep" || GREP_CMD="grep" +[[ $MACHINE == Darwin ]] && DIFF_CMD="$(brew --prefix)/bin/diff" || DIFF_CMD="diff" \ No newline at end of file diff --git a/src/lib/modules/generate.sh b/src/lib/modules/generate.sh new file mode 100644 index 0000000..2515af8 --- /dev/null +++ b/src/lib/modules/generate.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +####################################### +# Generate a .dropboxignore file based on .gitignore file. +# Globals: +# DROPBOX_IGNORE_FILE_NAME +# Arguments: +# .dropboxignore file path (even if not exists). +# Outputs: +# Status about the generated file. +####################################### +generate_dropboxignore_file() { + dropboxignore_file_path="$(dirname "$1")/$DROPBOX_IGNORE_FILE_NAME" + if [ -f "$dropboxignore_file_path" ]; then + log_debug "Already existing file: $(get_relative_path "$dropboxignore_file_path" "$BASE_FOLDER")" + else + tee "$dropboxignore_file_path" >/dev/null </dev/null + ;; + Darwin) + xattr -w "$FILE_ATTR_NAME" 1 "$1" >/dev/null + ;; + esac + log_debug "Ignored file: $(get_relative_path "$1" "$BASE_FOLDER")" + else + log_debug "Already ignored file: $(get_relative_path "$1" "$BASE_FOLDER")" + fi +} + +####################################### +# Get file status. +# Globals: +# FILE_ATTR_VALUE +# FILE_ATTR_NAME +# Arguments: +# Input file. +####################################### +file_ignore_status() { + unset FILE_ATTR_VALUE + case $MACHINE in + Linux) + FILE_ATTR_VALUE="$(getfattr --absolute-names --only-values -m "$FILE_ATTR_NAME" "$1")" + ;; + Darwin) + FILE_ATTR_VALUE="$(xattr -p "$FILE_ATTR_NAME" "$1" 2>/dev/null)" + ;; + esac +} \ No newline at end of file diff --git a/src/lib/modules/loader.sh b/src/lib/modules/loader.sh new file mode 100644 index 0000000..1e54d24 --- /dev/null +++ b/src/lib/modules/loader.sh @@ -0,0 +1,18 @@ +#!/bin/bash +# shellcheck disable=SC1090 +####################################### +# Source sub-commands scripts. +# Arguments: +# CLI directory. +####################################### +source_subcommands(){ + for subfolder in "modules" "commands"; + do + for f in $( find "$1/$subfolder" -type f \( -name "*.sh" -and -not -name "loader.sh" \) | sort -n ); + do + source "$f" + done + done +} + +source_subcommands "$1" \ No newline at end of file diff --git a/src/lib/modules/logging.sh b/src/lib/modules/logging.sh new file mode 100644 index 0000000..05da18d --- /dev/null +++ b/src/lib/modules/logging.sh @@ -0,0 +1,73 @@ +#!/bin/bash + +DEFAULT="\e[0m" +GREEN="\e[32m" +BLUE="\e[34m" +RED="\e[31m" +YELLOW="\e[33m" + +####################################### +# Log info message. +# Globals: +# VERBOSITY +# Arguments: +# Info Message. +# Outputs: +# Info Message +####################################### +log_info() { + if [ "$VERBOSITY" -ge 1 ]; then + echo -e "$(date) $GREEN [ INFO ] $1 $DEFAULT" + fi +} + +####################################### +# Log debug message. +# Globals: +# VERBOSITY +# Arguments: +# Debug Message. +# Outputs: +# Debug Message +####################################### +log_debug() { + if [ "$VERBOSITY" -ge 2 ]; then + echo -e "$(date) $BLUE [ DEBUG ] $1 $DEFAULT" + fi +} + +####################################### +# Log error message. +# Globals: +# VERBOSITY +# Arguments: +# Error Message. +# Exit status. +# Outputs: +# Error message. +####################################### +log_error() { + if [ "$VERBOSITY" -ge 0 ]; then + echo -e "$(date) $RED [ ERROR ] $1 $DEFAULT" + fi + if [ -z "$2" ]; then + exit 1 + else + exit "$2" + fi +} + +####################################### +# Log warning message. +# Globals: +# VERBOSITY +# Arguments: +# Warning message. +# Outputs: +# Warning message. +####################################### +log_warning() { + if [ "$VERBOSITY" -ge 1 ]; then + echo -e "$(date) $YELLOW [WARNING] $1 $DEFAULT" + fi +} \ No newline at end of file diff --git a/src/lib/modules/revert.sh b/src/lib/modules/revert.sh new file mode 100644 index 0000000..e03f6a4 --- /dev/null +++ b/src/lib/modules/revert.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +####################################### +# Revert ignored file. +# Arguments: +# Input file. +# Outputs: +# Message about the reverted file. +####################################### +revert_ignored() { + file_ignore_status "$1" + if [ "$FILE_ATTR_VALUE" == 1 ]; then + case $MACHINE in + Linux) + attr -r "$FILE_ATTR_NAME" "$1" >/dev/null + ;; + Darwin) + xattr -d "$FILE_ATTR_NAME" "$1" >/dev/null + ;; + esac + + log_debug "Reverted file: $(get_relative_path "$1" "$BASE_FOLDER")" + ((TOTAL_N_REVERTED_FILES++)) + else + log_debug "Already reverted file: $(get_relative_path "$1" "$BASE_FOLDER")" + fi +} \ No newline at end of file diff --git a/src/lib/modules/update.sh b/src/lib/modules/update.sh new file mode 100644 index 0000000..2f2efa8 --- /dev/null +++ b/src/lib/modules/update.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +####################################### +# uUpdate a .dropboxignore file based on changes on .gitignore file. +# Arguments: +# dropboxingore file path to update. +# Outputs: +# Update status. +####################################### +update_dropboxignore_file() { + diff_content=$("$DIFF_CMD" --new-line-format="" --unchanged-line-format="" --ignore-blank-lines --ignore-tab-expansion --ignore-space-change --ignore-trailing-space -I "# [Automatically|-]" "${1}" "${2}") + if [ -n "$diff_content" ]; then + tee -a "$2" >/dev/null </dev/null; then + log_debug "realpath command is installed" + elif command -v python &>/dev/null; then + log_debug "python is installed" + else + log_error "Neither realpath command not python could be found in you system" + fi + case $MACHINE in + Linux) + if ! command -v getfattr &>/dev/null; then + log_error "attr package is not installed" 5 + fi + if ! command -v attr &>/dev/null; then + log_error "attr package is not installed" 5 + fi + log_debug "attr package is installed" + + ;; + Darwin) + if ! command -v xattr &>/dev/null; then + log_error "xattr package not installed" 5 + fi + ;; + *) + log_error "$MACHINE is not supported" 3 + ;; + esac +} + +check_os + +####################################### +# Check input file or folder. +# Globals: +# DROPBOX_IGNORE_FILE_NAME +# BASE_FOLDER +# Arguments: +# Input file or folder. +# Outputs: +# Input file/folder or error if not exists. +# Returns: +# 0 if file or folder exists, otherwise, returns 2. +####################################### +check_input() { + if [ -z "$1" ]; then + log_error "You have to provide a file or folder" 2 + elif [ -d "$1" ]; then + log_debug "Input folder: \"$1\"" + BASE_FOLDER="$1" + elif [ -f "$1" ] || { [ "$(basename "$1")" == "$DROPBOX_IGNORE_FILE_NAME" ] && [ -d "$(dirname "$1")" ]; }; then + log_debug "Input file: \"$1\"" + BASE_FOLDER="$(dirname "$1")" + else + log_error "\"$1\" does not exists" 2 + fi +} + +####################################### +# Get absolute path from relative path. +# Arguments: +# Relative path. +####################################### +get_absolute_path() { + if command -v realpath &>/dev/null; then + realpath "$1" + else + python -c 'import os.path, sys;print(os.path.abspath(sys.argv[1]))' "$1" + fi +} + +####################################### +# Get relative path from absolute path. +# Arguments: +# Absolute path. +# Base Absolute path. +####################################### +get_relative_path() { + if command -v realpath &>/dev/null; then + realpath --relative-to="${2-$PWD}" "$1" + else + python -c 'import os.path, sys;print(os.path.relpath(sys.argv[1],sys.argv[2]))' "$1" "${2-$PWD}" + fi +} + +####################################### +# Find all .gitignore files. +# Globals: +# GIT_IGNORE_FILE_NAME +# Arguments: +# Input file or folder. +####################################### +find_gitignore_files() { + if [ -d "$1" ]; then + GITIGNORE_FILES=$(find "$1" -type f -name "$GIT_IGNORE_FILE_NAME") + else + GITIGNORE_FILES=$(find "$(dirname "$1")" -maxdepth 1 -type f -name "$GIT_IGNORE_FILE_NAME") + fi +} + +####################################### +# Find all .dropboxignore files. +# Globals: +# DROPBOX_IGNORE_FILE_NAME +# DROPBOX_IGNORE_FILES +# Arguments: +# Input folder or file. +####################################### +find_dropboxignore_files() { + DROPBOX_IGNORE_FILES=$(find "$1" -type f -name "$DROPBOX_IGNORE_FILE_NAME") +} diff --git a/utils/install.sh b/src/utils/install.sh similarity index 100% rename from utils/install.sh rename to src/utils/install.sh diff --git a/utils/stats.sh b/src/utils/stats.sh similarity index 100% rename from utils/stats.sh rename to src/utils/stats.sh diff --git a/tests/test_delete.bats b/tests/test_delete.bats new file mode 100644 index 0000000..c558d99 --- /dev/null +++ b/tests/test_delete.bats @@ -0,0 +1,40 @@ +#!/usr/bin/env bash +# shellcheck shell=bash +setup() { + load 'test_helper/common-setup' + _common_setup +} + +@test "Test delete command" { + mkdir "$BATS_TEST_TMPDIR/other" + touch "$BATS_TEST_TMPDIR/other/$DROPBOXIGNORE_NAME" + run $dropboxignore delete "$BATS_TEST_TMPDIR" + assert_success + assert_file_not_exist "$BATS_TEST_TMPDIR/$DROPBOXIGNORE_NAME" + assert_file_not_exist "$BATS_TEST_TMPDIR/other/$DROPBOXIGNORE_NAME" + assert_output --partial "Deleted files: 2" +} + +@test "Test cmd_delete" { + # Test success on existing path + run cmd_delete "$BATS_TEST_TMPDIR" + assert_success + assert_file_not_exist "$BATS_TEST_TMPDIR/$DROPBOXIGNORE_NAME" + + # Test success on specific dropboxignore file parameter + touch "$BATS_TEST_TMPDIR/$DROPBOXIGNORE_NAME" + run cmd_delete "$BATS_TEST_TMPDIR/$DROPBOXIGNORE_NAME" + assert_success + assert_file_not_exist "$BATS_TEST_TMPDIR/$DROPBOXIGNORE_NAME" + + # Test failure when passing non dropboxignore file + touch "$BATS_TEST_TMPDIR/a.txt" + run cmd_delete "$BATS_TEST_TMPDIR/a.txt" + assert_output --partial "Given file is not a .dropboxignore file." + assert_failure 1 + + # Test non existing file of path + run cmd_delete "$BATS_TEST_TMPDIR/nonexistingfolder/" + assert_output --partial "file not found" + assert_failure 3 +} \ No newline at end of file diff --git a/tests/test_generate.bats b/tests/test_generate.bats new file mode 100644 index 0000000..5059f17 --- /dev/null +++ b/tests/test_generate.bats @@ -0,0 +1,61 @@ +#!/usr/bin/env bash +# shellcheck shell=bash +setup() { + load 'test_helper/common-setup' + _common_setup +} + +@test "Test generate command when .dropboxignore file not exists" { + rm -rf "$BATS_TEST_TMPDIR/$DROPBOXIGNORE_NAME" + run $dropboxignore generate "$BATS_TEST_TMPDIR" + assert_success + assert_file_exist "$BATS_TEST_TMPDIR/$DROPBOXIGNORE_NAME" + assert_output --partial "Created file: .dropboxignore" + assert_output --partial "Total number of generated files: 1" +} + +@test "Test generate command when .dropboxignore file exists" { + run $dropboxignore generate "$BATS_TEST_TMPDIR" + assert_success + assert_file_exist "$BATS_TEST_TMPDIR/$DROPBOXIGNORE_NAME" + assert_output --partial "Total number of generated files: 0" +} + +@test "Test generate command when .gitignore file not exists" { + rm "$BATS_TEST_TMPDIR/$GITIGNORE_NAME" "$BATS_TEST_TMPDIR/$DROPBOXIGNORE_NAME" + run $dropboxignore generate "$BATS_TEST_TMPDIR" + assert_success + assert_file_not_exist "$BATS_TEST_TMPDIR/$DROPBOXIGNORE_NAME" + assert_file_not_exist "$BATS_TEST_TMPDIR/$GITIGNORE_NAME" + assert_output --partial "Total number of generated files: 0" +} + +@test "Test generate: don't create .dropboxignore file when exception patterns exists" { + rm "$BATS_TEST_TMPDIR/$DROPBOXIGNORE_NAME" + echo "a + !something" > "$BATS_TEST_TMPDIR/$GITIGNORE_NAME" + run $dropboxignore generate "$BATS_TEST_TMPDIR" + assert_success + assert_file_not_exist "$BATS_TEST_TMPDIR/$DROPBOXIGNORE_NAME" + assert_output --partial ".gitignore contains exception patterns, will be ignored" +} + +@test "Test generate_dropboxignore_file" { + echo "sample" > "$BATS_TEST_TMPDIR/$GITIGNORE_NAME" + rm "$BATS_TEST_TMPDIR/$DROPBOXIGNORE_NAME" + run generate_dropboxignore_file "$BATS_TEST_TMPDIR/$DROPBOXIGNORE_NAME" + echo "$BATS_TEST_TMPDIR/$GITIGNORE_NAME" + assert_file_contains "$BATS_TEST_TMPDIR/$DROPBOXIGNORE_NAME" "[sample]" +} + +@test "Test generate_dropboxignore_file already exists" { + echo "sample" > "$BATS_TEST_TMPDIR/$GITIGNORE_NAME" + rm "$BATS_TEST_TMPDIR/$DROPBOXIGNORE_NAME" + run generate_dropboxignore_file "$BATS_TEST_TMPDIR/$DROPBOXIGNORE_NAME" + echo "$BATS_TEST_TMPDIR/$GITIGNORE_NAME" + assert_file_contains "$BATS_TEST_TMPDIR/$DROPBOXIGNORE_NAME" "[sample]" + VERBOSITY=3 + run generate_dropboxignore_file "$BATS_TEST_TMPDIR/$DROPBOXIGNORE_NAME" + assert_output --partial "Already existing file:" + assert_output --partial "$BATS_TEST_TMPDIR/$DROPBOXIGNORE_NAME" +} \ No newline at end of file diff --git a/tests/test_genupi.bats b/tests/test_genupi.bats new file mode 100644 index 0000000..c326505 --- /dev/null +++ b/tests/test_genupi.bats @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +# shellcheck shell=bash +setup() { + load 'test_helper/common-setup' + _common_setup +} + +@test "Test genupi command" { + rm -rf "$BATS_TEST_TMPDIR/$DROPBOXIGNORE_NAME" + run $dropboxignore genupi "$BATS_TEST_TMPDIR" + assert_success + assert_file_exist "$BATS_TEST_TMPDIR/$DROPBOXIGNORE_NAME" + assert_file_exist "$BATS_TEST_TMPDIR/$GITIGNORE_NAME" + assert_output --partial "Total number of generated files: 1" +} \ No newline at end of file diff --git a/tests/test_help.bats b/tests/test_help.bats new file mode 100644 index 0000000..48d0505 --- /dev/null +++ b/tests/test_help.bats @@ -0,0 +1,18 @@ +#!/usr/bin/env bash +# shellcheck shell=bash +setup() { + load 'test_helper/common-setup' + _common_setup +} + +@test "Test help command" { + run $dropboxignore help + assert_success + run $dropboxignore + assert_success +} + +@test "Test help command without passing the sub-command" { + run $dropboxignore + assert_success +} \ No newline at end of file diff --git a/tests/test_helper/common-setup.bash b/tests/test_helper/common-setup.bash new file mode 100644 index 0000000..244fdf9 --- /dev/null +++ b/tests/test_helper/common-setup.bash @@ -0,0 +1,20 @@ +#!/usr/bin/env bash + +GITIGNORE_NAME=".gitignore" +DROPBOXIGNORE_NAME=".dropboxignore" + +_common_setup() { + load '../libs/bats-support/load' + load '../libs/bats-file/load' + load '../libs/bats-assert/load' + unset DROPBOXIGNORE_FILES + # shellcheck disable=SC1091 + source "src/lib/modules/loader.sh" "$BATS_TEST_DIRNAME/../src/lib" > /dev/null + # shellcheck disable=SC1091 + touch "$BATS_TEST_TMPDIR/$DROPBOXIGNORE_NAME" + touch "$BATS_TEST_TMPDIR/$GITIGNORE_NAME" + # shellcheck disable=SC2034 + dropboxignore="$BATS_TEST_DIRNAME/../src/bin/cli.sh" + chown -R "$USER:$GROUP" "$BATS_RUN_TMPDIR" + cd "$BATS_TMPDIR" || return +} \ No newline at end of file diff --git a/tests/test_ignore.bats b/tests/test_ignore.bats new file mode 100644 index 0000000..59383ed --- /dev/null +++ b/tests/test_ignore.bats @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +# shellcheck shell=bash +setup() { + load 'test_helper/common-setup' + _common_setup +} + +@test "Test ignore_files on .dropboxingore" { + touch "$BATS_TEST_TMPDIR/.dropboxignore" + run cmd_ignore "$BATS_TEST_TMPDIR/.dropboxignore" + assert_output --partial "Cannot ignore an .dropboxignore. Choose another file or folder." + assert_failure 4 +} + +@test "Test ignore_file on .dropboxingore" { + touch "$BATS_TEST_TMPDIR/.dropboxignore" + run ignore_file "$BATS_TEST_TMPDIR/.dropboxignore" + assert_success +} + +@test "Test ignore_files existing file" { + touch "$BATS_TEST_TMPDIR/foo.txt" + run cmd_ignore "$BATS_TEST_TMPDIR/foo.txt" + assert_output --partial "Total number of ignored files: 1" + assert_output --partial "Total number of ignored folders: 0" + assert_success +} \ No newline at end of file diff --git a/tests/test_list.bats b/tests/test_list.bats new file mode 100644 index 0000000..896cb56 --- /dev/null +++ b/tests/test_list.bats @@ -0,0 +1,20 @@ +#!/usr/bin/env bash +# shellcheck shell=bash +setup() { + load 'test_helper/common-setup' + _common_setup +} + +@test "Test list command without results" { + run $dropboxignore ignore "$BATS_RUN_TMPDIR" + assert_success + run $dropboxignore list "$BATS_RUN_TMPDIR" -v 3 -p '*' + assert_success + assert_output --partial "Total number of ignored files: 2" + assert_output --partial "Total number of ignored folders: 0" +} + +@test "Test list command non existing folder" { + run $dropboxignore list "$BATS_RUN_TMPDIR/non-existing" + assert_failure 2 +} \ No newline at end of file diff --git a/tests/test_logging.bats b/tests/test_logging.bats new file mode 100644 index 0000000..b7aeb44 --- /dev/null +++ b/tests/test_logging.bats @@ -0,0 +1,56 @@ +#!/usr/bin/env bash +# shellcheck shell=bash +setup() { + load 'test_helper/common-setup' + _common_setup +} + +@test "Test log_info high verbosity" { + VERBOSITY=1 + run log_info "test" + assert_output --partial "test" +} + +@test "Test log_info low verbosity" { + VERBOSITY=0 + run log_info "test" + refute_output --partial "test" +} + +@test "Test log_debug high verbosity" { + VERBOSITY=2 + run log_debug "test" + assert_output --partial "test" +} + +@test "Test log_debug low verbosity" { + VERBOSITY=0 + run log_debug "test" + refute_output --partial "test" +} + +@test "Test log_error high verbosity" { + VERBOSITY=2 + run log_error "test" + assert_output --partial "test" + assert_failure 1 +} + +@test "Test log_error low verbosity exit code" { + VERBOSITY=-1 + run log_error "test" 2 + refute_output --partial "test" + assert_failure 2 +} + +@test "Test log_warning high verbosity" { + VERBOSITY=1 + run log_warning "test" + assert_output --partial "test" +} + +@test "Test log_warning low verbosity" { + VERBOSITY=0 + run log_warning "test" + refute_output --partial "test" +} \ No newline at end of file diff --git a/tests/test_revert.bats b/tests/test_revert.bats new file mode 100644 index 0000000..1939e63 --- /dev/null +++ b/tests/test_revert.bats @@ -0,0 +1,29 @@ +#!/usr/bin/env bash +# shellcheck shell=bash +setup() { + load 'test_helper/common-setup' + _common_setup +} + +@test "Test revert_ignored_files specific file" { + touch "$BATS_TEST_TMPDIR/foo.txt" + run $dropboxignore ignore "$BATS_TEST_TMPDIR/foo.txt" + assert_output --partial "Total number of ignored files: 1" + assert_output --partial "Total number of ignored folders: 0" + assert_success + run $dropboxignore revert "$BATS_TEST_TMPDIR" + assert_output --partial "Total number of reverted files: 1" + assert_success +} + +@test "Test revert_ignored_files folder" { + touch "$BATS_TEST_TMPDIR/foo.txt" + echo "*.txt" > "$BATS_TEST_TMPDIR/.dropboxignore" + run $dropboxignore ignore . + assert_output --partial "Total number of ignored files: 1" + assert_output --partial "Total number of ignored folders: 0" + assert_success + run $dropboxignore revert . + assert_output --partial "Total number of reverted files: 3" + assert_success +} \ No newline at end of file diff --git a/tests/test_update.bats b/tests/test_update.bats new file mode 100644 index 0000000..239c4f1 --- /dev/null +++ b/tests/test_update.bats @@ -0,0 +1,21 @@ +#!/usr/bin/env bash +# shellcheck shell=bash +setup() { + load 'test_helper/common-setup' + _common_setup +} + +@test "Test update_dropboxignore_files" { + touch "$BATS_TEST_TMPDIR/.dropboxignore" + echo "*.txt" > "$BATS_TEST_TMPDIR/.gitignore" + run $dropboxignore update "$BATS_TEST_TMPDIR" + assert_output --partial "Updated .dropboxignore" +} + +@test "Test update_dropboxignore_file" { + touch "$BATS_TEST_TMPDIR/.dropboxignore" + echo "*.txt" > "$BATS_TEST_TMPDIR/.gitignore" + run update_dropboxignore_file "$BATS_TEST_TMPDIR/.gitignore" "$BATS_TEST_TMPDIR/.dropboxignore" + assert_output --partial "$BATS_TEST_TMPDIR/.dropboxignore" + assert_output --partial "Updated" +} \ No newline at end of file diff --git a/tests/test_utils.bats b/tests/test_utils.bats new file mode 100644 index 0000000..fd62853 --- /dev/null +++ b/tests/test_utils.bats @@ -0,0 +1,88 @@ +#!/usr/bin/env bash + +# shellcheck shell=bash +setup() { + load 'test_helper/common-setup' + _common_setup +} + +@test "Test check_input" { + # Test missing error on missing argument. + run check_input + assert_failure 2 + + # Test success when folder exists. + check_input "$BATS_TEST_TMPDIR" + assert_equal "$BASE_FOLDER" "$BATS_TEST_TMPDIR" + + # Test error on missing file + run check_input "$BATS_TEST_TMPDIR/missingfile.txt" + assert_failure 2 + + # Test error on missing folder. + run check_input "missing_folder/" + assert_failure 2 + + # Test succes when passing missing `.dropboxignore` file as parameter. + run check_input "$BATS_TEST_TMPDIR/$DROPBOXIGNORE_NAME" + assert_success +} + +@test "Test find_gitignore_files" { + rm "$BATS_TEST_TMPDIR/$GITIGNORE_NAME" + # Test path without files + find_gitignore_files "$BATS_TEST_TMPDIR" + assert_equal "" "$GITIGNORE_FILES" + + # Test path with file + touch "$BATS_TEST_TMPDIR/$GITIGNORE_NAME" + find_gitignore_files "$BATS_TEST_TMPDIR" + assert_equal "$BATS_TEST_TMPDIR/$GITIGNORE_NAME" "$GITIGNORE_FILES" + + # Test path with multiple files + mkdir "$BATS_TEST_TMPDIR/f" + touch "$BATS_TEST_TMPDIR/f/$GITIGNORE_NAME" + find_gitignore_files "$BATS_TEST_TMPDIR" + run echo "$GITIGNORE_FILES" + assert_output --partial "$BATS_TEST_TMPDIR/f/$GITIGNORE_NAME" + run echo "$GITIGNORE_FILES" + assert_output --partial "$BATS_TEST_TMPDIR/$GITIGNORE_NAME" + # Test file with max depth + find_gitignore_files "$BATS_TEST_TMPDIR/$GITIGNORE_NAME" + assert_equal "$BATS_TEST_TMPDIR/$GITIGNORE_NAME" "$GITIGNORE_FILES" +} + +@test "Test find_dropboxignore_files" { + find_dropboxignore_files "$BATS_TEST_TMPDIR" + assert_equal "$DROPBOX_IGNORE_FILES" "$BATS_TEST_TMPDIR/$DROPBOXIGNORE_NAME" +} + +@test "Test check_os Linux" { + MACHINE=Linux + run check_os + assert_success +} + +@test "Test check_os OSX" { + MACHINE=Darwin + run check_os + assert_success +} + +@test "Test check_os Unknown" { + MACHINE=Unknown + run check_os + assert_output --partial "Unknown is not supported" + assert_failure 3 +} + +@test "Test unknown command" { + run $dropboxignore foo + assert_output --partial "dropboxignore: 'foo' is not a dropboxignore command" + assert_failure 1 +} + +@test "Test unknown option" { + run $dropboxignore list -a + assert_failure 2 +} \ No newline at end of file diff --git a/tests/test_version.bats b/tests/test_version.bats new file mode 100644 index 0000000..8b976cc --- /dev/null +++ b/tests/test_version.bats @@ -0,0 +1,11 @@ +#!/usr/bin/env bash +# shellcheck shell=bash +setup() { + load 'test_helper/common-setup' + _common_setup +} + +@test "Test version command" { + run $dropboxignore version + assert_success +} \ No newline at end of file diff --git a/tests/tests.bats b/tests/tests.bats deleted file mode 100755 index c172d17..0000000 --- a/tests/tests.bats +++ /dev/null @@ -1,336 +0,0 @@ -#!/usr/bin/env bats -# shellcheck shell=bash - -load '../libs/bats-support/load' -load '../libs/bats-file/load' -load '../libs/bats-assert/load' - -TEST_FOLDER="testing_folder" -GITIGNORE_NAME=".gitignore" -DROPBOXIGNORE_NAME=".dropboxignore" - -setup () { - unset DROPBOXIGNORE_FILES - rm -rf "$TEST_FOLDER" - mkdir "$TEST_FOLDER" - # shellcheck disable=SC1091 - source bin/dropboxignore.sh > /dev/null - touch "$TEST_FOLDER/$DROPBOXIGNORE_NAME" - touch "$TEST_FOLDER/$GITIGNORE_NAME" - dropboxignore='./bin/dropboxignore.sh' -} - -teardown () { rm -rf "$TEST_FOLDER"; } - -@test "Test help command" { - run $dropboxignore help > /dev/null - assert_success - run $dropboxignore - assert_success -} - -@test "Test version command" { - run $dropboxignore version - assert_success - assert_output --partial "$(grep -oP '^VERSION=(.*)$' bin/dropboxignore.sh | sed -r 's/^VERSION=//')" -} - -@test "Test generate command when .dropboxignore file not exists" { - rm "$TEST_FOLDER/$DROPBOXIGNORE_NAME" - run $dropboxignore generate "$TEST_FOLDER" - assert_success - assert_file_exist "$TEST_FOLDER/$DROPBOXIGNORE_NAME" - assert_output --partial "Created file: .dropboxignore" - assert_output --partial "Total number of generated files: 1" -} - -@test "Test generate command when .dropboxignore file exists" { - run $dropboxignore generate "$TEST_FOLDER" - assert_success - assert_file_exist "$TEST_FOLDER/$DROPBOXIGNORE_NAME" - assert_output --partial "Total number of generated files: 0" -} - -@test "Test generate command when .gitignore file not exists" { - rm "$TEST_FOLDER/$GITIGNORE_NAME" "$TEST_FOLDER/$DROPBOXIGNORE_NAME" - run $dropboxignore generate "$TEST_FOLDER" - assert_success - assert_file_not_exist "$TEST_FOLDER/$DROPBOXIGNORE_NAME" - assert_file_not_exist "$TEST_FOLDER/$GITIGNORE_NAME" - assert_output --partial "Total number of generated files: 0" -} - -@test "Test generate: don't create .dropboxignore file when exception patterns exists" { - rm "$TEST_FOLDER/$DROPBOXIGNORE_NAME" - echo "a - !something" > "$TEST_FOLDER/$GITIGNORE_NAME" - run $dropboxignore generate "$TEST_FOLDER" - assert_success - assert_file_not_exist "$TEST_FOLDER/$DROPBOXIGNORE_NAME" - assert_output --partial ".gitignore contains exception patterns, will be ignored" -} - -@test "Test delete command" { - mkdir "$TEST_FOLDER/other" - touch "$TEST_FOLDER/other/$DROPBOXIGNORE_NAME" - run $dropboxignore delete "$TEST_FOLDER" - assert_success - assert_file_not_exist "$TEST_FOLDER/$DROPBOXIGNORE_NAME" - assert_file_not_exist "$TEST_FOLDER/other/$DROPBOXIGNORE_NAME" - assert_output --partial "Deleted files: 2" -} - -@test "Test list command without results" { - run $dropboxignore ignore "$TEST_FOLDER" -p '*' - assert_success - run $dropboxignore list "$TEST_FOLDER" -v 3 - assert_success - assert_output --partial "Total number of ignored files: 0" - assert_output --partial "Total number of ignored folders: 0" -} - -@test "Test list command non existing folder" { - run $dropboxignore list "$TEST_FOLDER/non-existing" - assert_failure 2 -} - -@test "Test check_input" { - # Test missing error on missing argument. - run check_input - assert_failure 2 - - # Test success when folder exists. - check_input "$TEST_FOLDER" - assert_equal "$BASE_FOLDER" "$TEST_FOLDER" - - # Test error on missing file - run check_input "$TEST_FOLDER/missingfile.txt" - assert_failure 2 - - # Test error on missing folder. - run check_input "missing_folder/" - assert_failure 2 - - # Test succes when passing missing `.dropboxignore` file as parameter. - run check_input "$TEST_FOLDER/$DROPBOXIGNORE_NAME" - assert_success -} - -@test "Test find_gitignore_files" { - rm "$TEST_FOLDER/$GITIGNORE_NAME" - # Test path without files - find_gitignore_files "$TEST_FOLDER" - assert_equal "" "$GITIGNORE_FILES" - - # Test path with file - touch "$TEST_FOLDER/$GITIGNORE_NAME" - find_gitignore_files "$TEST_FOLDER" - assert_equal "$TEST_FOLDER/$GITIGNORE_NAME" "$GITIGNORE_FILES" - - # Test path with multiple files - mkdir "$TEST_FOLDER/f" - touch "$TEST_FOLDER/f/$GITIGNORE_NAME" - find_gitignore_files "$TEST_FOLDER" - run echo "$GITIGNORE_FILES" - assert_output --partial "$TEST_FOLDER/f/$GITIGNORE_NAME" - run echo "$GITIGNORE_FILES" - assert_output --partial "$TEST_FOLDER/$GITIGNORE_NAME" - # Test file with max depth - find_gitignore_files "$TEST_FOLDER/$GITIGNORE_NAME" - assert_equal "$TEST_FOLDER/$GITIGNORE_NAME" "$GITIGNORE_FILES" -} - -@test "Test delete_dropboxignore_files" { - # Test success on existing path - run delete_dropboxignore_files "$TEST_FOLDER" - assert_success - assert_file_not_exist "$TEST_FOLDER/$DROPBOXIGNORE_NAME" - - # Test success on specific dropboxignore file parameter - touch "$TEST_FOLDER/$DROPBOXIGNORE_NAME" - run delete_dropboxignore_files "$TEST_FOLDER/$DROPBOXIGNORE_NAME" - assert_success - assert_file_not_exist "$TEST_FOLDER/$DROPBOXIGNORE_NAME" - - # Test failure when passing non dropboxignore file - touch "$TEST_FOLDER/a.txt" - run delete_dropboxignore_files "$TEST_FOLDER/a.txt" - assert_output --partial "Given file is not a .dropboxignore file." - assert_failure 1 - - # Test non existing file of path - run delete_dropboxignore_files "$TEST_FOLDER/nonexistingfolder/" - assert_output --partial "file not found" - assert_failure 3 -} - -@test "Test find_dropboxignore_files" { - find_dropboxignore_files "$TEST_FOLDER" - assert_equal "$DROPBOX_IGNORE_FILES" "$TEST_FOLDER/$DROPBOXIGNORE_NAME" -} - -@test "Test generate_dropboxignore_file" { - echo "sample" > "$TEST_FOLDER/$GITIGNORE_NAME" - rm "$TEST_FOLDER/$DROPBOXIGNORE_NAME" - run generate_dropboxignore_file "$TEST_FOLDER/$DROPBOXIGNORE_NAME" - echo "$TEST_FOLDER/$GITIGNORE_NAME" - assert_file_contains "$TEST_FOLDER/$DROPBOXIGNORE_NAME" "[sample]" -} - -@test "Test generate_dropboxignore_file already exists" { - echo "sample" > "$TEST_FOLDER/$GITIGNORE_NAME" - rm "$TEST_FOLDER/$DROPBOXIGNORE_NAME" - run generate_dropboxignore_file "$TEST_FOLDER/$DROPBOXIGNORE_NAME" - echo "$TEST_FOLDER/$GITIGNORE_NAME" - assert_file_contains "$TEST_FOLDER/$DROPBOXIGNORE_NAME" "[sample]" - VERBOSITY=3 - run generate_dropboxignore_file "$TEST_FOLDER/$DROPBOXIGNORE_NAME" - assert_output --partial "Already existing file: testing_folder/.dropboxignore" -} - -@test "Test genupi command" { - run $dropboxignore genupi "$TEST_FOLDER" - assert_success - assert_file_exist "$TEST_FOLDER/$DROPBOXIGNORE_NAME" - assert_file_exist "$TEST_FOLDER/$GITIGNORE_NAME" - assert_output --partial "Total number of generated files: 0" -} - -@test "Test log_info high verbosity" { - VERBOSITY=1 - run log_info "test" - assert_output --partial "test" -} - -@test "Test log_info low verbosity" { - VERBOSITY=0 - run log_info "test" - refute_output --partial "test" -} - -@test "Test log_debug high verbosity" { - VERBOSITY=2 - run log_debug "test" - assert_output --partial "test" -} - -@test "Test log_debug low verbosity" { - VERBOSITY=0 - run log_debug "test" - refute_output --partial "test" -} - -@test "Test log_error high verbosity" { - VERBOSITY=2 - run log_error "test" - assert_output --partial "test" - assert_failure 1 -} - -@test "Test log_error low verbosity exit code" { - VERBOSITY=-1 - run log_error "test" 2 - refute_output --partial "test" - assert_failure 2 -} - -@test "Test log_warning high verbosity" { - VERBOSITY=1 - run log_warning "test" - assert_output --partial "test" -} - -@test "Test log_warning low verbosity" { - VERBOSITY=0 - run log_warning "test" - refute_output --partial "test" -} - -@test "Test check_os Linux" { - MACHINE=Linux - run check_os - assert_success -} - -@test "Test check_os OSX" { - MACHINE=Darwin - run check_os - assert_success -} - -@test "Test check_os Unknown" { - MACHINE=Unknown - run check_os - assert_output --partial "Unknown is not supported" - assert_failure 3 -} - -@test "Test unknown command" { - run $dropboxignore foo - assert_output --partial "dropboxignore.sh: 'foo' is not a dropboxignore.sh command." - assert_output --partial "See 'dropboxignore.sh help" - assert_failure 1 -} - -@test "Test unknown option" { - run $dropboxignore list -a - assert_failure 2 -} - -@test "Test update_dropboxignore_files" { - touch "$TEST_FOLDER/.dropboxignore" - echo "*.txt" > "$TEST_FOLDER/.gitignore" - run $dropboxignore update "$TEST_FOLDER" - assert_output --partial "Updated .dropboxignore" -} - -@test "Test update_dropboxignore_file" { - touch "$TEST_FOLDER/.dropboxignore" - echo "*.txt" > "$TEST_FOLDER/.gitignore" - run update_dropboxignore_file "$TEST_FOLDER/.gitignore" "$TEST_FOLDER/.dropboxignore" - assert_output --partial "Updated testing_folder/.dropboxignore" -} - -@test "Test ignore_files on .dropboxingore" { - touch "$TEST_FOLDER/.dropboxignore" - run ignore_files "$TEST_FOLDER/.dropboxignore" - assert_output --partial "Cannot ignore an .dropboxignore. Choose another file or folder." - assert_failure 4 -} - -@test "Test ignore_file on .dropboxingore" { - touch "$TEST_FOLDER/.dropboxignore" - run ignore_file "$TEST_FOLDER/.dropboxignore" - assert_success -} - -@test "Test ignore_files existing file" { - touch "$TEST_FOLDER/foo.txt" - run ignore_files "$TEST_FOLDER/foo.txt" - assert_output --partial "Total number of ignored files: 1" - assert_output --partial "Total number of ignored folders: 0" - assert_success -} - -@test "Test revert_ignored_files specific file" { - touch "$TEST_FOLDER/foo.txt" - run $dropboxignore ignore "$TEST_FOLDER/foo.txt" - assert_output --partial "Total number of ignored files: 1" - assert_output --partial "Total number of ignored folders: 0" - assert_success - run $dropboxignore revert . - assert_output --partial "Total number of reverted files: 1" - assert_success -} - -@test "Test revert_ignored_files folder" { - touch "$TEST_FOLDER/foo.txt" - echo "*.txt" > "$TEST_FOLDER/.dropboxignore" - run $dropboxignore ignore . - assert_output --partial "Total number of ignored files: 1" - assert_output --partial "Total number of ignored folders: 0" - assert_success - run $dropboxignore revert . - assert_output --partial "Total number of reverted files: 1" - assert_success -} \ No newline at end of file