Merge pull request #10289 from dolthub/tim/fix-rerquires-repo

Consistently implement which commands require a repo
This commit is contained in:
Tim Sehn
2026-01-08 13:27:44 -08:00
committed by GitHub
25 changed files with 260 additions and 95 deletions

View File

@@ -143,6 +143,7 @@ SKIP_SERVER_TESTS=$(cat <<-EOM
~nonlocal.bats~
~branch-activity.bats~
~mutual-tls-auth.bats~
~requires-repo.bats~
EOM
)

View File

@@ -273,7 +273,7 @@ teardown() {
# Assert that the current directory has NOT been initialized
run dolt status
[ $status -eq 1 ]
[ $status -eq 2 ]
[[ $output =~ "The current directory is not a valid dolt repository" ]] || false
[ ! -d "$baseDir/not_a_repo/.dolt" ]

View File

@@ -0,0 +1,223 @@
#!/usr/bin/env bats
# Tests for CLI behavior when running commands from a parent directory
# that contains a child dolt database directory.
# See https://github.com/dolthub/dolt/issues/10230
load $BATS_TEST_DIRNAME/helper/common.bash
load $BATS_TEST_DIRNAME/helper/query-server-common.bash
setup() {
skiponwindows "tests are flaky on Windows"
if [ "$SQL_ENGINE" = "remote-engine" ]; then
skip "This test tests local CLI behavior, SQL_ENGINE is not needed."
fi
setup_no_dolt_init
# Create a child database directory
mkdir child_db
cd child_db
dolt init
dolt sql -q "CREATE TABLE test_table (pk INT PRIMARY KEY, value VARCHAR(100))"
dolt add .
dolt commit -m "Initial commit"
cd ..
# We are now in the parent directory with a child dolt database
}
teardown() {
stop_sql_server 1 && sleep 0.5
teardown_common
}
NOT_VALID_REPO_ERROR="The current directory is not a valid dolt repository."
# =============================================================================
# Tests WITHOUT a running SQL server
# All commands that require a repo should fail consistently from parent directory
# =============================================================================
@test "requires-repo: dolt status from parent dir without server fails" {
run dolt status
[ "$status" -ne 0 ]
[ "${lines[0]}" = "$NOT_VALID_REPO_ERROR" ]
}
@test "requires-repo: dolt checkout from parent dir without server fails" {
run dolt checkout -b new_branch
[ "$status" -ne 0 ]
[ "${lines[0]}" = "$NOT_VALID_REPO_ERROR" ]
}
@test "requires-repo: dolt branch from parent dir without server fails" {
run dolt branch
[ "$status" -ne 0 ]
[ "${lines[0]}" = "$NOT_VALID_REPO_ERROR" ]
}
@test "requires-repo: dolt log from parent dir without server fails" {
run dolt log
[ "$status" -ne 0 ]
[ "${lines[0]}" = "$NOT_VALID_REPO_ERROR" ]
}
@test "requires-repo: dolt diff from parent dir without server fails" {
run dolt diff
[ "$status" -ne 0 ]
[ "${lines[0]}" = "$NOT_VALID_REPO_ERROR" ]
}
@test "requires-repo: dolt add from parent dir without server fails" {
run dolt add .
[ "$status" -ne 0 ]
[ "${lines[0]}" = "$NOT_VALID_REPO_ERROR" ]
}
@test "requires-repo: dolt commit from parent dir without server fails" {
run dolt commit -m "test"
[ "$status" -ne 0 ]
[ "${lines[0]}" = "$NOT_VALID_REPO_ERROR" ]
}
# =============================================================================
# Tests WITH a running SQL server
# Commands should still fail from parent directory - they require a local repo
# =============================================================================
@test "requires-repo: dolt status from parent dir with running server fails" {
start_multi_db_server child_db
# Status should fail - it requires being in a dolt repo directory
run dolt status
[ "$status" -ne 0 ]
[[ "$output" =~ "$NOT_VALID_REPO_ERROR" ]] || false
}
@test "requires-repo: dolt checkout from parent dir with running server fails" {
start_multi_db_server child_db
run dolt checkout -b new_branch
[ "$status" -ne 0 ]
[[ "$output" =~ "$NOT_VALID_REPO_ERROR" ]] || false
}
@test "requires-repo: dolt branch from parent dir with running server fails" {
start_multi_db_server child_db
run dolt branch
[ "$status" -ne 0 ]
[[ "$output" =~ "$NOT_VALID_REPO_ERROR" ]] || false
}
@test "requires-repo: dolt log from parent dir with running server fails" {
start_multi_db_server child_db
run dolt log
[ "$status" -ne 0 ]
[[ "$output" =~ "$NOT_VALID_REPO_ERROR" ]] || false
}
@test "requires-repo: dolt diff from parent dir with running server fails" {
start_multi_db_server child_db
run dolt diff
[ "$status" -ne 0 ]
[[ "$output" =~ "$NOT_VALID_REPO_ERROR" ]] || false
}
# =============================================================================
# Commands that do NOT require a repo should work from parent directory
# =============================================================================
@test "requires-repo: dolt sql from parent dir with running server succeeds" {
start_multi_db_server child_db
# SQL commands should work by connecting to the running server
run dolt sql -q "SELECT * FROM child_db.test_table"
[ "$status" -eq 0 ]
}
@test "requires-repo: dolt version from parent dir succeeds" {
run dolt version
[ "$status" -eq 0 ]
[[ "$output" =~ "dolt version" ]] || false
}
@test "requires-repo: dolt init in new dir succeeds" {
# Test that dolt init works in a new directory (doesn't require existing repo)
mkdir new_init_test
cd new_init_test
run dolt init
[ "$status" -eq 0 ]
[[ "$output" =~ "Successfully initialized dolt data repository" ]] || false
cd ..
rm -rf new_init_test
}
# =============================================================================
# Commands should work correctly when run from within the child database directory
# =============================================================================
@test "requires-repo: commands in child directory work normally" {
cd child_db
run dolt status
[ "$status" -eq 0 ]
[[ "$output" =~ "On branch main" ]] || false
run dolt checkout -b new_branch
[ "$status" -eq 0 ]
run dolt branch
[ "$status" -eq 0 ]
[[ "$output" =~ "new_branch" ]] || false
[[ "$output" =~ "main" ]] || false
run dolt log
[ "$status" -eq 0 ]
[[ "$output" =~ "Initial commit" ]] || false
run dolt diff
[ "$status" -eq 0 ]
}
# =============================================================================
# Test consistent behavior - all repo-requiring commands should fail the same way
# =============================================================================
@test "requires-repo: all repo-requiring commands fail consistently" {
# All these commands should fail with the same error
for cmd in "status" "branch" "log" "diff" "add ." "commit -m test" "checkout -b test"; do
run dolt $cmd
[ "$status" -ne 0 ]
[[ "${lines[0]}" = "$NOT_VALID_REPO_ERROR" ]] || false
done
}
# =============================================================================
# Tests with --use-db or --host flags bypass repo requirement
# These flags indicate a manual database/server connection, so the local
# directory check should be bypassed.
# =============================================================================
@test "requires-repo: --host and --use-db flags bypass repo requirement for log" {
start_multi_db_server child_db
# With --host and --use-db, log should work from parent directory
run dolt --host 127.0.0.1 --port $PORT --no-tls --use-db child_db log
[ "$status" -eq 0 ]
[[ "$output" =~ "Initial commit" ]] || false
}
@test "requires-repo: --host and --use-db flags bypass repo requirement for branch" {
start_multi_db_server child_db
run dolt --host 127.0.0.1 --port $PORT --no-tls --use-db child_db branch
[ "$status" -eq 0 ]
[[ "$output" =~ "main" ]] || false
}
@test "requires-repo: --host and --use-db flags bypass repo requirement for diff" {
start_multi_db_server child_db
run dolt --host 127.0.0.1 --port $PORT --no-tls --use-db child_db diff
[ "$status" -eq 0 ]
}

View File

@@ -63,6 +63,7 @@ seed_and_start_serial_remote() {
cd clones
dolt sql -q "call dolt_clone('--depth', '1','http://localhost:50051/test-org/test-repo')"
cd test-repo
run dolt log --oneline --decorate=no
[ "$status" -eq 0 ]
@@ -85,6 +86,7 @@ seed_and_start_serial_remote() {
cd clones
run dolt sql -q "call dolt_clone('--depth', '2','http://localhost:50051/test-org/test-repo')"
[ "$status" -eq 0 ]
cd test-repo
run dolt log --oneline --decorate=no
[ "$status" -eq 0 ]
@@ -111,6 +113,7 @@ seed_and_start_serial_remote() {
cd clones
run dolt sql -q "call dolt_clone('--depth', '1','file://../file-remote')"
[ "$status" -eq 0 ]
cd file-remote
run dolt log --oneline --decorate=no
[ "$status" -eq 0 ]

View File

@@ -199,9 +199,11 @@ get_commit_hash_at() {
dolt sql -q "insert into test values (2), (3)"
dolt add test
dolt commit -m "insert more values into test"
cd ..
cd ..
start_sql_server altDB
cd altDB
run dolt blame test
[ "$status" -eq 0 ]
export out="$output"
@@ -269,6 +271,7 @@ get_commit_hash_at() {
@test "sql-local-remote: test 'status' and switch between server/no server" {
start_sql_server defaultDB
cd defaultDB
run dolt status
[ "$status" -eq 0 ] || false
@@ -323,9 +326,10 @@ get_commit_hash_at() {
dolt sql -q "create table test1 (pk int primary key)"
dolt sql -q "create table test2 (pk int primary key)"
dolt add test1
cd ..
cd ..
start_sql_server altDB
cd altDB
run dolt --verbose-engine-setup commit -m "committing remotely"
[ "$status" -eq 0 ]
@@ -333,20 +337,16 @@ get_commit_hash_at() {
stop_sql_server 1
cd altDB
run dolt log
[ "$status" -eq 0 ]
[[ "$output" =~ "committing remotely" ]] || false
run dolt add test2
[ "$status" -eq 0 ]
cd ..
run dolt --verbose-engine-setup commit -m "committing locally"
[ "$status" -eq 0 ]
[[ "$output" =~ "starting local mode" ]] || false
cd altDB
run dolt log
[ "$status" -eq 0 ]
[[ "$output" =~ "committing locally" ]] || false
@@ -691,6 +691,8 @@ SQL
@test "sql-local-remote: verify simple dolt reset behavior" {
start_sql_server altDB
cd altDB
dolt sql -q "create table test1 (pk int primary key)"
dolt add test1
dolt commit -m "create table test1"
@@ -781,20 +783,23 @@ SQL
[ $status -eq 0 ]
[[ "$output" =~ 'Revert "Commit ABCDEF"' ]] || false
dolt reset --hard HEAD~1
dolt --use-db altDB reset --hard HEAD~1
stop_sql_server 1
run dolt revert HEAD
run dolt --use-db altDB revert HEAD
[ $status -eq 0 ]
[[ $output =~ 'Revert "Commit ABCDEF"' ]] || false
}
@test "sql-local-remote: Ensure that dolt clean works for each mode" {
cd altDB
dolt reset --hard
dolt sql -q "create table tbl (pk int primary key)"
cd ..
start_sql_server altDB
cd altDB
run dolt --verbose-engine-setup clean --dry-run
[ $status -eq 0 ]