diff mbox series

[v2,1/2] t7501: add tests for --include, --only of commit

Message ID 20240109165304.8027-4-shyamthakkar001@gmail.com (mailing list archive)
State New, archived
Headers show
Series [v2,1/2] t7501: add tests for --include, --only of commit | expand

Commit Message

Ghanshyam Thakkar Jan. 9, 2024, 4:51 p.m. UTC
This commit adds tests for testing --include (-o) and --only (-o). It
include testing --include with and without staged changes, testing
--only with and without staged changes and testing --only and --include
together.

Some tests already exist in t7501 for testing --only, however, they
are only tested in combination with --amend --allow-empty and to-be-born
branch, and not for testing --only separately.

Signed-off-by: Ghanshyam Thakkar <shyamthakkar001@gmail.com>
---
A single test also exists in t1092-sparse-checkout-compatibility.sh
named "commit including unstaged changes" for --include, however, that
compares the results of sparse-checkout with full-checkout when using
--include and is not necessarily for testing --include itself. Otherthan
that, I could not find any other test for --include.

 t/t7501-commit-basic-functionality.sh | 55 ++++++++++++++++++++++++++-
 1 file changed, 54 insertions(+), 1 deletion(-)

Comments

Junio C Hamano Jan. 9, 2024, 5:50 p.m. UTC | #1
Ghanshyam Thakkar <shyamthakkar001@gmail.com> writes:

> This commit adds tests for testing --include (-o) and --only (-o). It

"-i" not "-o" stands for "--include".

Please write in imperative mood.

> +test_expect_success '--include fails with untracked files' '
> +	echo content >baz &&
> +	test_must_fail git commit --include baz -m initial
> +'

My comment on argument order applies to this iteration, too.  Please
write your code to help readers.

> +test_expect_success 'commit --include' '
> +	test_when_finished "rm -rf remaining" &&

Why recursive removal when you _know_ what you create is a plan
file?
diff mbox series

Patch

diff --git a/t/t7501-commit-basic-functionality.sh b/t/t7501-commit-basic-functionality.sh
index 3d8500a52e..9325db1f66 100755
--- a/t/t7501-commit-basic-functionality.sh
+++ b/t/t7501-commit-basic-functionality.sh
@@ -3,7 +3,7 @@ 
 # Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>
 #
 
-# FIXME: Test the various index usages, -i and -o, test reflog,
+# FIXME: Test the various index usages, test reflog,
 # signoff
 
 test_description='git commit'
@@ -150,6 +150,59 @@  test_expect_success 'setup: commit message from file' '
 	git commit -F msg -a
 '
 
+test_expect_success '--include fails with untracked files' '
+	echo content >baz &&
+	test_must_fail git commit --include baz -m initial
+'
+
+test_expect_success 'commit --include' '
+	test_when_finished "rm -rf remaining" &&
+	echo content >file &&
+	git commit --include file -m "file" &&
+	git diff --name-only >remaining &&
+	test_must_be_empty remaining
+'
+
+test_expect_success '--include with staged changes' '
+	echo newcontent >baz &&
+	echo newcontent >file &&
+	git add file &&
+	git commit --include baz -m "file baz" &&
+
+	git diff --name-only >remaining &&
+	test_must_be_empty remaining &&
+	git diff --name-only --staged >remaining &&
+	test_must_be_empty remaining
+'
+
+test_expect_success 'commit --only' '
+	echo change >file &&
+	git commit --only file -m "file" &&
+	git diff --name-only >actual &&
+	test_must_be_empty actual
+'
+
+test_expect_success '--only fails with untracked files' '
+	echo content >newfile &&
+	test_must_fail git commit --only newfile -m "newfile"
+'
+
+test_expect_success '--only excludes staged changes' '
+	echo content >file &&
+	echo content >baz &&
+	git add baz &&
+	git commit --only file -m "file" &&
+	git diff --name-only --staged >actual &&
+	test_grep "baz" actual
+'
+
+test_expect_success '--include and --only together fails' '
+	test_when_finished "git reset --hard" &&
+	echo new >file &&
+	echo new >baz &&
+	test_must_fail git commit --include baz --only bar -m "bar"
+'
+
 test_expect_success 'amend commit' '
 	cat >editor <<-\EOF &&
 	#!/bin/sh