Skip to content

Commit

Permalink
test: use tmp_path pytest fixture over tmpdir
Browse files Browse the repository at this point in the history
A note from pytest doc: these days, it is preferred to use tmp_path
and tmp_path_factory.
  • Loading branch information
psergee committed Jul 2, 2024
1 parent e66c474 commit 94ba971
Show file tree
Hide file tree
Showing 31 changed files with 1,159 additions and 1,161 deletions.
48 changes: 24 additions & 24 deletions test/integration/binaries/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
from utils import config_name, run_command_and_get_output


def test_list(tt_cmd, tmpdir):
def test_list(tt_cmd, tmp_path):
# Copy the test bin_dir to the "run" directory.
test_app_path = os.path.join(os.path.dirname(__file__), "bin")
shutil.copytree(test_app_path, tmpdir + "/bin", True)
shutil.copytree(test_app_path, tmp_path / "bin", True)

configPath = os.path.join(tmpdir, config_name)
configPath = tmp_path / config_name
# Create test config
with open(configPath, 'w') as f:
f.write('tt:\n env:\n bin_dir: "./bin"\n inc_dir:\n')
Expand All @@ -19,7 +19,7 @@ def test_list(tt_cmd, tmpdir):
binaries_cmd = [tt_cmd, "--cfg", configPath, "binaries", "list"]
binaries_process = subprocess.Popen(
binaries_cmd,
cwd=tmpdir,
cwd=tmp_path,
stderr=subprocess.STDOUT,
stdout=subprocess.PIPE,
text=True
Expand All @@ -35,8 +35,8 @@ def test_list(tt_cmd, tmpdir):
assert "2.8.1 [active]" in output


def test_list_no_directory(tt_cmd, tmpdir):
configPath = os.path.join(tmpdir, config_name)
def test_list_no_directory(tt_cmd, tmp_path):
configPath = tmp_path / config_name
# Create test config
with open(configPath, 'w') as f:
f.write('tt:\n env:\n bin_dir: "./bin"\n inc_dir:\n')
Expand All @@ -45,7 +45,7 @@ def test_list_no_directory(tt_cmd, tmpdir):
binaries_cmd = [tt_cmd, "--cfg", configPath, "binaries", "list"]
binaries_process = subprocess.Popen(
binaries_cmd,
cwd=tmpdir,
cwd=tmp_path,
stderr=subprocess.STDOUT,
stdout=subprocess.PIPE,
text=True
Expand All @@ -57,9 +57,9 @@ def test_list_no_directory(tt_cmd, tmpdir):
assert "there are no binaries installed in this environment of 'tt'" in output


def test_list_empty_directory(tt_cmd, tmpdir):
configPath = os.path.join(tmpdir, config_name)
os.mkdir(tmpdir+"/bin")
def test_list_empty_directory(tt_cmd, tmp_path):
configPath = tmp_path / config_name
os.mkdir(tmp_path / "bin")
# Create test config
with open(configPath, 'w') as f:
f.write('tt:\n env:\n bin_dir: "./bin"\n inc_dir:\n')
Expand All @@ -68,7 +68,7 @@ def test_list_empty_directory(tt_cmd, tmpdir):
binaries_cmd = [tt_cmd, "--cfg", configPath, "binaries", "list"]
binaries_process = subprocess.Popen(
binaries_cmd,
cwd=tmpdir,
cwd=tmp_path,
stderr=subprocess.STDOUT,
stdout=subprocess.PIPE,
text=True
Expand All @@ -80,12 +80,12 @@ def test_list_empty_directory(tt_cmd, tmpdir):
assert "there are no binaries installed in this environment of 'tt'" in output


def test_list_tarantool_dev(tt_cmd, tmpdir):
def test_list_tarantool_dev(tt_cmd, tmp_path):
# Copy the test dir to the "run" directory.
test_app_path = os.path.join(os.path.dirname(__file__), "tarantool_dev")
shutil.copytree(test_app_path, tmpdir + "/tarantool_dev", True)
shutil.copytree(test_app_path, tmp_path / "tarantool_dev", True)

config_path = os.path.join(tmpdir, config_name)
config_path = tmp_path / config_name
# Create test config.
with open(config_path, "w") as f:
f.write(
Expand All @@ -94,7 +94,7 @@ def test_list_tarantool_dev(tt_cmd, tmpdir):
binaries_cmd = [tt_cmd, "--cfg", config_path, "binaries", "list"]
binaries_process = subprocess.Popen(
binaries_cmd,
cwd=tmpdir,
cwd=tmp_path,
stderr=subprocess.STDOUT,
stdout=subprocess.PIPE,
text=True
Expand All @@ -108,25 +108,25 @@ def test_list_tarantool_dev(tt_cmd, tmpdir):
assert "tarantool-dev" in output


def test_list_tarantool_no_symlink(tt_cmd, tmpdir):
def test_list_tarantool_no_symlink(tt_cmd, tmp_path):
# Copy the test bin_dir to the "run" directory.
test_app_path = os.path.join(os.path.dirname(__file__), "bin")
shutil.copytree(test_app_path, tmpdir + "/bin", True)
shutil.copytree(test_app_path, tmp_path / "bin", True)

configPath = os.path.join(tmpdir, config_name)
configPath = tmp_path / config_name
# Create test config
with open(configPath, 'w') as f:
f.write('tt:\n env:\n bin_dir: "./bin"\n inc_dir:\n')

os.remove(os.path.join(tmpdir, "bin", "tarantool"))
with open(os.path.join(tmpdir, "bin", "tarantool"), "w") as tnt_file:
os.remove(tmp_path / "bin" / "tarantool")
with open(tmp_path / "bin" / "tarantool", "w") as tnt_file:
tnt_file.write("""#!/bin/sh
echo 'Tarantool 3.1.0-entrypoint-83-gcb0264c3c'""")
os.chmod(os.path.join(tmpdir, "bin", "tarantool"), 0o750)
os.chmod(tmp_path / "bin" / "tarantool", 0o750)

# Print binaries
binaries_cmd = [tt_cmd, "--cfg", configPath, "binaries", "list"]
rc, output = run_command_and_get_output(binaries_cmd, cwd=tmpdir)
rc, output = run_command_and_get_output(binaries_cmd, cwd=tmp_path)

assert rc == 0
assert "tt" in output
Expand All @@ -137,9 +137,9 @@ def test_list_tarantool_no_symlink(tt_cmd, tmpdir):
assert "3.1.0-entrypoint-83-gcb0264c3c [active]" in output

# Remove non-versioned tarantool binary.
os.remove(os.path.join(tmpdir, "bin", "tarantool"))
os.remove(tmp_path / "bin" / "tarantool")
binaries_cmd = [tt_cmd, "--cfg", configPath, "binaries", "list"]
rc, output = run_command_and_get_output(binaries_cmd, cwd=tmpdir)
rc, output = run_command_and_get_output(binaries_cmd, cwd=tmp_path)

assert rc == 0
assert "tt" in output
Expand Down
34 changes: 17 additions & 17 deletions test/integration/binaries/test_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
from utils import config_name


def test_switch(tt_cmd, tmpdir):
def test_switch(tt_cmd, tmp_path):
# Copy test files.
testdata_path = os.path.join(
os.path.dirname(__file__),
"testdata/test_tarantool"
)
shutil.copytree(testdata_path, os.path.join(tmpdir, "testdata"), True)
testdata_path = os.path.join(tmpdir, "testdata")
shutil.copytree(testdata_path, tmp_path / "testdata", True)
testdata_path = tmp_path / "testdata"

tt_dir = os.path.join(testdata_path, "tt")

Expand Down Expand Up @@ -45,14 +45,14 @@ def test_switch(tt_cmd, tmpdir):
assert tarantool_inc == expected_inc


def test_switch_with_link(tt_cmd, tmpdir):
def test_switch_with_link(tt_cmd, tmp_path):
# Copy test files.
testdata_path = os.path.join(
os.path.dirname(__file__),
"testdata/test_tarantool_link"
)
shutil.copytree(testdata_path, os.path.join(tmpdir, "testdata"), True)
testdata_path = os.path.join(tmpdir, "testdata")
shutil.copytree(testdata_path, tmp_path / "testdata", True)
testdata_path = tmp_path / "testdata"

tt_dir = os.path.join(testdata_path, "tt")

Expand Down Expand Up @@ -83,14 +83,14 @@ def test_switch_with_link(tt_cmd, tmpdir):
assert tarantool_inc == expected_inc


def test_switch_invalid_program(tt_cmd, tmpdir):
def test_switch_invalid_program(tt_cmd, tmp_path):
# Copy test files.
testdata_path = os.path.join(
os.path.dirname(__file__),
"testdata/test_tarantool"
)
shutil.copytree(testdata_path, os.path.join(tmpdir, "testdata"), True)
testdata_path = os.path.join(tmpdir, "testdata")
shutil.copytree(testdata_path, tmp_path / "testdata", True)
testdata_path = tmp_path / "testdata"

tt_dir = os.path.join(testdata_path, "tt")

Expand All @@ -112,11 +112,11 @@ def test_switch_invalid_program(tt_cmd, tmpdir):
assert install_process_rc != 0


def test_switch_tt(tt_cmd, tmpdir):
config_path = os.path.join(tmpdir, "tt.yaml")
bin_dir_path = os.path.join(tmpdir, "bin")
def test_switch_tt(tt_cmd, tmp_path):
config_path = tmp_path / "tt.yaml"
bin_dir_path = tmp_path / "bin"
with open(config_path, "w") as f:
yaml.dump({"env": {"bin_dir": bin_dir_path}}, f)
yaml.dump({"env": {"bin_dir": bin_dir_path.as_posix()}}, f)

fake_tt_path = os.path.join(bin_dir_path, "tt_v7.7.7")
os.makedirs(bin_dir_path)
Expand Down Expand Up @@ -144,11 +144,11 @@ def test_switch_tt(tt_cmd, tmpdir):
assert tt_bin == expected_bin


def test_switch_tt_full_version_name(tt_cmd, tmpdir):
config_path = os.path.join(tmpdir, "tt.yaml")
bin_dir_path = os.path.join(tmpdir, "bin")
def test_switch_tt_full_version_name(tt_cmd, tmp_path):
config_path = tmp_path / "tt.yaml"
bin_dir_path = tmp_path / "bin"
with open(config_path, "w") as f:
yaml.dump({"env": {"bin_dir": bin_dir_path}}, f)
yaml.dump({"env": {"bin_dir": bin_dir_path.as_posix()}}, f)

fake_tt_path = os.path.join(bin_dir_path, "tt_v7.7.7")
os.makedirs(bin_dir_path)
Expand Down
20 changes: 10 additions & 10 deletions test/integration/cat/test_cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,47 @@
from utils import run_command_and_get_output


def test_cat_unset_arg(tt_cmd, tmpdir):
def test_cat_unset_arg(tt_cmd, tmp_path):
# Testing with unset .xlog or .snap file.
cmd = [tt_cmd, "cat"]
rc, output = run_command_and_get_output(cmd, cwd=tmpdir)
rc, output = run_command_and_get_output(cmd, cwd=tmp_path)
assert rc == 1
assert re.search(r"it is required to specify at least one .xlog or .snap file", output)


def test_cat_non_existent_file(tt_cmd, tmpdir):
def test_cat_non_existent_file(tt_cmd, tmp_path):
# Testing with non-existent .xlog or .snap file.
cmd = [tt_cmd, "cat", "path-to-non-existent-file"]
rc, output = run_command_and_get_output(cmd, cwd=tmpdir)
rc, output = run_command_and_get_output(cmd, cwd=tmp_path)
assert rc == 1
assert re.search(r"No such file or directory", output)


def test_cat_snap_file(tt_cmd, tmpdir):
def test_cat_snap_file(tt_cmd, tmp_path):
# Copy the .snap file to the "run" directory.
test_app_path = os.path.join(os.path.dirname(__file__), "test_file", "test.snap")
shutil.copy(test_app_path, tmpdir)
shutil.copy(test_app_path, tmp_path)

# Testing .snap file.
cmd = [
tt_cmd, "cat", "test.snap", "--show-system",
"--space=320", "--space=296", "--from=423", "--to=513"
]
rc, output = run_command_and_get_output(cmd, cwd=tmpdir)
rc, output = run_command_and_get_output(cmd, cwd=tmp_path)
assert rc == 0
assert re.search(r"lsn: 423", output)
assert re.search(r"lsn: 512", output)
assert re.search(r"space_id: 320", output)
assert re.search(r"space_id: 296", output)


def test_cat_xlog_file(tt_cmd, tmpdir):
def test_cat_xlog_file(tt_cmd, tmp_path):
# Copy the .xlog file to the "run" directory.
test_app_path = os.path.join(os.path.dirname(__file__), "test_file", "test.xlog")
shutil.copy(test_app_path, tmpdir)
shutil.copy(test_app_path, tmp_path)

# Testing .xlog file.
cmd = [tt_cmd, "cat", "test.xlog", "--show-system", "--replica=1"]
rc, output = run_command_and_get_output(cmd, cwd=tmpdir)
rc, output = run_command_and_get_output(cmd, cwd=tmp_path)
assert rc == 0
assert re.search(r"replica_id: 1", output)
Loading

0 comments on commit 94ba971

Please sign in to comment.