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

feat: add tests for local wsh file copy cmd #1911

Merged
merged 3 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 12 additions & 0 deletions tests/copytests/cases/test000.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# copy a file to one with a different name
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Missing Shebang Directive
Consider adding a shebang (e.g., #!/bin/bash) at the beginning of the file to specify the shell interpreter and improve portability.

🧰 Tools
🪛 Shellcheck (0.10.0)

[error] 1-1: Tips depend on target shell and yours is unknown. Add a shebang or a 'shell' directive.

(SC2148)

# ensure that the original exists
set -e
cd "$HOME/testcp"
touch foo.txt

wsh file copy foo.txt bar.txt

if [ ! -f foo.txt ]; then
echo "foo.txt does not exist"
exit 1
fi
12 changes: 12 additions & 0 deletions tests/copytests/cases/test001.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# copy a file to one with a different name
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Missing Shebang Directive
A shebang line (e.g., #!/bin/bash) at the top of the script is recommended to clearly define the interpreter and avoid any shell compatibility issues.

🧰 Tools
🪛 Shellcheck (0.10.0)

[error] 1-1: Tips depend on target shell and yours is unknown. Add a shebang or a 'shell' directive.

(SC2148)

# ensure that the destination file exists
set -e
cd "$HOME/testcp"
touch foo.txt

wsh file copy foo.txt bar.txt

if [ ! -f bar.txt ]; then
echo "bar.txt does not exist"
exit 1
fi
18 changes: 18 additions & 0 deletions tests/copytests/cases/test002.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# copy a file with contents
# ensure the contents are the same
set -e
cd "$HOME/testcp"
touch foo.txt
echo "The quick brown fox jumps over the lazy dog" > foo.txt

wsh file copy foo.txt bar.txt


FOO_MD5=$(md5sum foo.txt | cut -d " " -f1)
BAR_MD5=$(md5sum bar.txt | cut -d " " -f1)
if [ $FOO_MD5 != $BAR_MD5 ]; then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Quote Variables in Conditional Check
To avoid potential word-splitting issues or errors when variables are empty, ensure that the MD5 variables are quoted in the conditional statement.

Proposed change:

-if [ $FOO_MD5 != $BAR_MD5 ]; then
+if [ "$FOO_MD5" != "$BAR_MD5" ]; then
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if [ $FOO_MD5 != $BAR_MD5 ]; then
if [ "$FOO_MD5" != "$BAR_MD5" ]; then

echo "files are not the same"
echo "FOO_MD5 is $FOO_MD5"
echo "BAR_MD5 is $BAR_MD5"
exit 1
fi
12 changes: 12 additions & 0 deletions tests/copytests/cases/test003.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# copy a file where source starts with ./
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Missing Shebang Directive
It’s best practice to add a shebang (e.g., #!/bin/bash) at the very top of the script. This ensures that the intended shell is used and helps static analysis tools (Shellcheck SC2148) to correctly interpret the script.

🧰 Tools
🪛 Shellcheck (0.10.0)

[error] 1-1: Tips depend on target shell and yours is unknown. Add a shebang or a 'shell' directive.

(SC2148)

# ensure the source file exists
set -e
cd "$HOME/testcp"
touch foo.txt

wsh file copy ./foo.txt bar.txt

if [ ! -f foo.txt ]; then
echo "foo.txt does not exist"
exit 1
fi
12 changes: 12 additions & 0 deletions tests/copytests/cases/test004.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# copy a file where source starts with ./
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Missing Shebang Directive
Adding a shebang (e.g., #!/bin/bash) at the top will ensure clarity regarding the script’s execution environment and address static analysis hints.

🧰 Tools
🪛 Shellcheck (0.10.0)

[error] 1-1: Tips depend on target shell and yours is unknown. Add a shebang or a 'shell' directive.

(SC2148)

# ensure the destination file exists
set -e
cd "$HOME/testcp"
touch foo.txt

wsh file copy ./foo.txt bar.txt

if [ ! -f bar.txt ]; then
echo "bar.txt does not exist"
exit 1
fi
13 changes: 13 additions & 0 deletions tests/copytests/cases/test005.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# copy a file where destination starts with ./
# ensure the source file exists

Comment on lines +1 to +3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add Shebang for Script Clarity
It's recommended to add a shebang line (e.g. #!/bin/bash) at the top of the file to explicitly specify the interpreter. This helps avoid any ambiguity regarding the target shell and resolves the shellcheck warning (SC2148).

🧰 Tools
🪛 Shellcheck (0.10.0)

[error] 1-1: Tips depend on target shell and yours is unknown. Add a shebang or a 'shell' directive.

(SC2148)

set -e
cd "$HOME/testcp"
touch foo.txt

wsh file copy foo.txt ./bar.txt

if [ ! -f foo.txt ]; then
echo "foo.txt does not exist"
exit 1
fi
13 changes: 13 additions & 0 deletions tests/copytests/cases/test006.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# copy a file where destination starts with ./
# ensure the destination file exists

set -e
cd "$HOME/testcp"
touch foo.txt

wsh file copy foo.txt ./bar.txt

if [ ! -f bar.txt ]; then
echo "bar.txt does not exist"
exit 1
fi
13 changes: 13 additions & 0 deletions tests/copytests/cases/test007.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# copy a file where source and destination start with ./
# ensure the source file exists

set -e
cd "$HOME/testcp"
touch foo.txt

wsh file copy ./foo.txt ./bar.txt

if [ ! -f foo.txt ]; then
echo "foo.txt does not exist"
exit 1
fi
Comment on lines +10 to +13
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Improve validation checks for copy success
The current conditional block only checks for the existence of "foo.txt", ensuring that the source file was not removed. However, to fully validate the copy operation, you should also verify the existence (and optionally the content integrity) of "bar.txt".

For example, you could add the following snippet after line 9:

+if [ ! -f bar.txt ]; then
+    echo "bar.txt does not exist"
+    exit 1
+fi
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if [ ! -f foo.txt ]; then
echo "foo.txt does not exist"
exit 1
fi
if [ ! -f bar.txt ]; then
echo "bar.txt does not exist"
exit 1
fi
if [ ! -f foo.txt ]; then
echo "foo.txt does not exist"
exit 1
fi

13 changes: 13 additions & 0 deletions tests/copytests/cases/test008.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# copy a file where source and destination start with ./
# ensure the destination file exists

set -e
cd "$HOME/testcp"
touch foo.txt

wsh file copy ./foo.txt ./bar.txt

if [ ! -f bar.txt ]; then
echo "bar.txt does not exist"
exit 1
fi
13 changes: 13 additions & 0 deletions tests/copytests/cases/test009.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# copy a file to itself with the same literal name
# ensure the operation fails and the file still exists

set -e
cd "$HOME/testcp"
touch foo.txt

wsh file copy foo.txt foo.txt >/dev/null 2>&1 && echo "copy should have failed" && exit 1

if [ ! -f foo.txt ]; then
echo "foo.txt does not exist"
exit 1
fi
13 changes: 13 additions & 0 deletions tests/copytests/cases/test010.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# copy a file to itself with a different literal name
# ensure the copy fails and the file still exists

set -e
cd "$HOME/testcp"
touch foo.txt

wsh file copy foo.txt ./foo.txt >/dev/null 2>&1 && echo "copy should have failed" && exit 1

if [ ! -f foo.txt ]; then
echo "foo.txt does not exist"
exit 1
fi
13 changes: 13 additions & 0 deletions tests/copytests/cases/test011.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# copy a file with ~ used to resolve the source
# ensure the source still exists

set -e
cd "$HOME/testcp"
touch foo.txt

wsh file copy ~/testcp/foo.txt bar.txt

if [ ! -f foo.txt ]; then
echo "foo.txt does not exist"
exit 1
fi
13 changes: 13 additions & 0 deletions tests/copytests/cases/test012.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# copy a file with ~ used to resolve the source
# ensure the destination exists

set -e
cd "$HOME/testcp"
touch foo.txt

wsh file copy ~/testcp/foo.txt bar.txt

if [ ! -f bar.txt ]; then
echo "bar.txt does not exist"
exit 1
fi
13 changes: 13 additions & 0 deletions tests/copytests/cases/test013.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# copy a file with ~ used to resolve the destination
# ensure the source exists

set -e
cd "$HOME/testcp"
touch foo.txt

wsh file copy foo.txt ~/testcp/bar.txt

if [ ! -f foo.txt ]; then
echo "foo.txt does not exist"
exit 1
fi
13 changes: 13 additions & 0 deletions tests/copytests/cases/test014.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# copy a file with ~ used to resolve the destination
# ensure the destination exists

set -e
cd "$HOME/testcp"
touch foo.txt

wsh file copy foo.txt ~/testcp/bar.txt

if [ ! -f bar.txt ]; then
echo "bar.txt does not exist"
exit 1
fi
13 changes: 13 additions & 0 deletions tests/copytests/cases/test015.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# copy a file where source and destination are resolved with ~
# ensure the source file exists

set -e
cd "$HOME/testcp"
touch foo.txt

wsh file copy ~/testcp/foo.txt ~/testcp/bar.txt

if [ ! -f foo.txt ]; then
echo "foo.txt does not exist"
exit 1
fi
13 changes: 13 additions & 0 deletions tests/copytests/cases/test016.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# copy a file where source and destination are resolved with ~
# ensure the destination file exists

set -e
cd "$HOME/testcp"
touch foo.txt

wsh file copy ~/testcp/foo.txt ~/testcp/bar.txt

if [ ! -f bar.txt ]; then
echo "bar.txt does not exist"
exit 1
fi
13 changes: 13 additions & 0 deletions tests/copytests/cases/test017.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# copy a file to itself with ~ for destination resolution
# ensure that the operation fails and the file still exists

set -e
cd "$HOME/testcp"
touch foo.txt

wsh file copy foo.txt ~/testcp/foo.txt >/dev/null 2>&1 && echo "copy should have failed" && exit 1

if [ ! -f foo.txt ]; then
echo "foo.txt does not exist"
exit 1
fi
13 changes: 13 additions & 0 deletions tests/copytests/cases/test018.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# copy a file to itself with ~ for source resolution
# ensure that the operation fails and the file still exists

set -e
cd "$HOME/testcp"
touch foo.txt

wsh file copy ~/testcp/foo.txt foo.txt >/dev/null 2>&1 && echo "copy should have failed" && exit 1

if [ ! -f foo.txt ]; then
echo "foo.txt does not exist"
exit 1
fi
13 changes: 13 additions & 0 deletions tests/copytests/cases/test019.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# copy a file to itself with env var expansion in destination
# ensure the operation fails and the file still exists

set -e
cd "$HOME/testcp"
touch foo.txt

wsh file copy foo.txt "${HOME}"/testcp/foo.txt >/dev/null 2>&1 && echo "copy should have failed" && exit 1

if [ ! -f foo.txt ]; then
echo "foo.txt does not exist"
exit 1
fi
13 changes: 13 additions & 0 deletions tests/copytests/cases/test020.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# copy a file to itself with env var expansion in source
# ensure the operation fails and the file still exists

set -e
cd "$HOME/testcp"
touch foo.txt

wsh file copy "${HOME}"/testcp/foo.txt foo.txt >/dev/null 2>&1 && echo "copy should have failed" && exit 1

if [ ! -f foo.txt ]; then
echo "foo.txt does not exist"
exit 1
fi
14 changes: 14 additions & 0 deletions tests/copytests/cases/test021.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# copy to a deeper directory and rename
# ensure the destination file exists

set -e
cd "$HOME/testcp"
touch foo.txt
mkdir baz

wsh file copy foo.txt baz/bar.txt

if [ ! -f baz/bar.txt ]; then
echo "baz/bar.txt does not exist"
exit 1
fi
14 changes: 14 additions & 0 deletions tests/copytests/cases/test022.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# copy a file to a deeper directory with the same base name
# ensure the destination file exists

set -e
cd "$HOME/testcp"
touch foo.txt
mkdir baz

wsh file copy foo.txt baz/foo.txt

if [ ! -f baz/foo.txt ]; then
echo "baz/foo.txt does not exist"
exit 1
fi
14 changes: 14 additions & 0 deletions tests/copytests/cases/test023.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# copy into an existing directory ending in /
# ensure the file is inserted in the directory

set -e
cd "$HOME/testcp"
touch foo.txt
mkdir baz

wsh file copy foo.txt baz/

if [ ! -f baz/foo.txt ]; then
echo "baz/foo.txt does not exist"
exit 1
fi
14 changes: 14 additions & 0 deletions tests/copytests/cases/test024.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# copy into an existing directory not ending in /
# ensure the file is inserted in the directory

set -e
cd "$HOME/testcp"
touch foo.txt
mkdir baz

wsh file copy foo.txt baz

if [ ! -f baz/foo.txt ]; then
echo "baz/foo.txt does not exist"
exit 1
fi
15 changes: 15 additions & 0 deletions tests/copytests/cases/test025.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# copy into an non-existing directory where file has the same base name
# ensure the file is copied to a file inside the directory
# note that this is not regular cp behavior

set -e
cd "$HOME/testcp"
touch foo.txt

# this is different from cp behavior
wsh file copy foo.txt baz/foo.txt

if [ ! -f baz/foo.txt ]; then
echo "baz/foo.txt does not exist"
exit 1
fi
15 changes: 15 additions & 0 deletions tests/copytests/cases/test026.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# copy into an non-existing directory ending with a /
# ensure the file is copied to a file inside the directory
# note that this is not regular cp behavior

set -e
cd "$HOME/testcp"
touch foo.txt

# this is different from cp behavior
wsh file copy foo.txt baz/ >/dev/null 2>&1 && echo "command should have failed" && exit 1

if [ -f baz/foo.txt ]; then
echo "baz/foo.txt should not exist"
exit 1
fi
Loading
Loading