Skip to content

Commit 8b8b257

Browse files
authored
tmuxp freeze: Allow overwriting file via --overwrite (#618)
Thank you @betoSolares!
2 parents fd355f5 + 551ed77 commit 8b8b257

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

tests/test_cli.py

+36
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,14 @@ def test_import_tmuxinator(cli_args, inputs, tmpdir, monkeypatch):
520520
['\n', 'y\n', './la.yaml\n', 'y\n'],
521521
),
522522
(['freeze'], ['\n', 'y\n', './exists.yaml\n', './la.yaml\n', 'y\n']), # Exists
523+
( # Create a new one
524+
['freeze', 'mysession', '--force'],
525+
['\n', 'y\n', './la.yaml\n', 'y\n']
526+
),
527+
( # Imply current session if not entered
528+
['freeze', '--force'],
529+
['\n', 'y\n', './la.yaml\n', 'y\n'],
530+
),
523531
],
524532
)
525533
def test_freeze(server, cli_args, inputs, tmpdir, monkeypatch):
@@ -537,6 +545,34 @@ def test_freeze(server, cli_args, inputs, tmpdir, monkeypatch):
537545
assert tmpdir.join('la.yaml').check()
538546

539547

548+
@pytest.mark.parametrize(
549+
"cli_args,inputs",
550+
[
551+
( # Overwrite
552+
['freeze', 'mysession', '--force'],
553+
['\n', 'y\n', './exists.yaml\n', 'y\n'],
554+
),
555+
( # Imply current session if not entered
556+
['freeze', '--force'],
557+
['\n', 'y\n', './exists.yaml\n', 'y\n']
558+
),
559+
],
560+
)
561+
def test_freeze_overwrite(server, cli_args, inputs, tmpdir, monkeypatch):
562+
monkeypatch.setenv('HOME', str(tmpdir))
563+
tmpdir.join('exists.yaml').ensure()
564+
565+
server.new_session(session_name='mysession')
566+
567+
with tmpdir.as_cwd():
568+
runner = CliRunner()
569+
# Use tmux server (socket name) used in the test
570+
cli_args = cli_args + ['-L', server.socket_name]
571+
out = runner.invoke(cli.cli, cli_args, input=''.join(inputs))
572+
print(out.output)
573+
assert tmpdir.join('exists.yaml').check()
574+
575+
540576
def test_get_abs_path(tmpdir):
541577
expect = str(tmpdir)
542578
with tmpdir.as_cwd():

tmuxp/cli.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,8 @@ def startup(config_dir):
665665
@click.argument('session_name', nargs=1, required=False)
666666
@click.option('-S', 'socket_path', help='pass-through for tmux -S')
667667
@click.option('-L', 'socket_name', help='pass-through for tmux -L')
668-
def command_freeze(session_name, socket_name, socket_path):
668+
@click.option('--force', 'force', help='overwrite the config file', is_flag=True)
669+
def command_freeze(session_name, socket_name, socket_path, force):
669670
"""Snapshot a session into a config.
670671
671672
If SESSION_NAME is provided, snapshot that session. Otherwise, use the
@@ -722,7 +723,7 @@ def command_freeze(session_name, socket_name, socket_path):
722723
dest_prompt = click.prompt(
723724
'Save to: %s' % save_to, value_proc=get_abs_path, default=save_to
724725
)
725-
if os.path.exists(dest_prompt):
726+
if not force and os.path.exists(dest_prompt):
726727
print('%s exists. Pick a new filename.' % dest_prompt)
727728
continue
728729

0 commit comments

Comments
 (0)