2016-06-07 21:02:11 +02:00
|
|
|
import re
|
2021-10-30 12:16:09 -04:00
|
|
|
import tempfile
|
|
|
|
|
from os import path
|
2022-02-21 18:30:09 -05:00
|
|
|
from typing import Dict, Optional
|
2016-06-07 21:02:11 +02:00
|
|
|
|
2016-07-11 19:54:34 +02:00
|
|
|
import asciinema.config as cfg
|
2022-02-21 18:30:09 -05:00
|
|
|
from asciinema.config import Config
|
2016-06-07 21:02:11 +02:00
|
|
|
|
|
|
|
|
|
2022-02-21 18:30:09 -05:00
|
|
|
def create_config(
|
|
|
|
|
content: Optional[str] = None, env: Optional[Dict[str, str]] = None
|
|
|
|
|
) -> Config:
|
2021-10-30 13:21:45 -04:00
|
|
|
# avoid redefining `dir` builtin
|
|
|
|
|
dir_ = tempfile.mkdtemp()
|
2016-06-07 21:02:11 +02:00
|
|
|
|
2017-12-04 00:25:50 +01:00
|
|
|
if content:
|
2021-10-30 13:21:45 -04:00
|
|
|
# avoid redefining `os.path`
|
|
|
|
|
path_ = f"{dir_}/config"
|
|
|
|
|
with open(path_, "wt", encoding="utf_8") as f:
|
2017-12-04 00:25:50 +01:00
|
|
|
f.write(content)
|
2016-06-07 21:02:11 +02:00
|
|
|
|
2021-10-30 13:21:45 -04:00
|
|
|
return cfg.Config(dir_, env)
|
2016-06-07 21:02:11 +02:00
|
|
|
|
2016-10-14 00:45:41 +02:00
|
|
|
|
2022-02-21 18:30:09 -05:00
|
|
|
def read_install_id(install_id_path: str) -> str:
|
2021-10-30 13:21:45 -04:00
|
|
|
with open(install_id_path, "rt", encoding="utf_8") as f:
|
2017-12-04 00:25:50 +01:00
|
|
|
return f.read().strip()
|
2016-06-07 21:02:11 +02:00
|
|
|
|
2016-07-11 19:54:34 +02:00
|
|
|
|
2022-02-21 18:30:09 -05:00
|
|
|
def test_upgrade_no_config_file() -> None:
|
2017-12-04 00:25:50 +01:00
|
|
|
config = create_config()
|
|
|
|
|
config.upgrade()
|
|
|
|
|
install_id = read_install_id(config.install_id_path)
|
|
|
|
|
|
2021-10-30 13:21:45 -04:00
|
|
|
assert re.match("^\\w{8}-\\w{4}-\\w{4}-\\w{4}-\\w{12}", install_id)
|
|
|
|
|
assert install_id == config.install_id
|
2017-12-04 00:25:50 +01:00
|
|
|
assert not path.exists(config.config_file_path)
|
|
|
|
|
|
|
|
|
|
# it must not change after another upgrade
|
|
|
|
|
|
|
|
|
|
config.upgrade()
|
|
|
|
|
|
2021-10-30 13:21:45 -04:00
|
|
|
assert read_install_id(config.install_id_path) == install_id
|
2017-12-04 00:25:50 +01:00
|
|
|
|
|
|
|
|
|
2022-02-21 18:30:09 -05:00
|
|
|
def test_upgrade_config_file_with_api_token() -> None:
|
2017-12-04 00:25:50 +01:00
|
|
|
config = create_config("[api]\ntoken = foo-bar-baz")
|
|
|
|
|
config.upgrade()
|
|
|
|
|
|
2021-10-30 13:21:45 -04:00
|
|
|
assert read_install_id(config.install_id_path) == "foo-bar-baz"
|
|
|
|
|
assert config.install_id == "foo-bar-baz"
|
2017-12-04 00:25:50 +01:00
|
|
|
assert not path.exists(config.config_file_path)
|
|
|
|
|
|
|
|
|
|
config.upgrade()
|
|
|
|
|
|
2021-10-30 13:21:45 -04:00
|
|
|
assert read_install_id(config.install_id_path) == "foo-bar-baz"
|
2017-12-04 00:25:50 +01:00
|
|
|
|
|
|
|
|
|
2022-02-21 18:30:09 -05:00
|
|
|
def test_upgrade_config_file_with_api_token_and_more() -> None:
|
2021-10-30 13:21:45 -04:00
|
|
|
config = create_config(
|
|
|
|
|
"[api]\ntoken = foo-bar-baz\nurl = http://example.com"
|
|
|
|
|
)
|
2017-12-04 00:25:50 +01:00
|
|
|
config.upgrade()
|
|
|
|
|
|
2021-10-30 13:21:45 -04:00
|
|
|
assert read_install_id(config.install_id_path) == "foo-bar-baz"
|
|
|
|
|
assert config.install_id == "foo-bar-baz"
|
|
|
|
|
assert config.api_url == "http://example.com"
|
2017-12-04 00:25:50 +01:00
|
|
|
assert path.exists(config.config_file_path)
|
|
|
|
|
|
|
|
|
|
config.upgrade()
|
|
|
|
|
|
2021-10-30 13:21:45 -04:00
|
|
|
assert read_install_id(config.install_id_path) == "foo-bar-baz"
|
2017-12-04 00:25:50 +01:00
|
|
|
|
|
|
|
|
|
2022-02-21 18:30:09 -05:00
|
|
|
def test_upgrade_config_file_with_user_token() -> None:
|
2017-12-04 00:25:50 +01:00
|
|
|
config = create_config("[user]\ntoken = foo-bar-baz")
|
|
|
|
|
config.upgrade()
|
|
|
|
|
|
2021-10-30 13:21:45 -04:00
|
|
|
assert read_install_id(config.install_id_path) == "foo-bar-baz"
|
|
|
|
|
assert config.install_id == "foo-bar-baz"
|
2017-12-04 00:25:50 +01:00
|
|
|
assert not path.exists(config.config_file_path)
|
|
|
|
|
|
|
|
|
|
config.upgrade()
|
|
|
|
|
|
2021-10-30 13:21:45 -04:00
|
|
|
assert read_install_id(config.install_id_path) == "foo-bar-baz"
|
2017-12-04 00:25:50 +01:00
|
|
|
|
|
|
|
|
|
2022-02-21 18:30:09 -05:00
|
|
|
def test_upgrade_config_file_with_user_token_and_more() -> None:
|
2021-10-30 13:21:45 -04:00
|
|
|
config = create_config(
|
|
|
|
|
"[user]\ntoken = foo-bar-baz\n[api]\nurl = http://example.com"
|
|
|
|
|
)
|
2017-12-04 00:25:50 +01:00
|
|
|
config.upgrade()
|
|
|
|
|
|
2021-10-30 13:21:45 -04:00
|
|
|
assert read_install_id(config.install_id_path) == "foo-bar-baz"
|
|
|
|
|
assert config.install_id == "foo-bar-baz"
|
|
|
|
|
assert config.api_url == "http://example.com"
|
2017-12-04 00:25:50 +01:00
|
|
|
assert path.exists(config.config_file_path)
|
|
|
|
|
|
|
|
|
|
config.upgrade()
|
|
|
|
|
|
2021-10-30 13:21:45 -04:00
|
|
|
assert read_install_id(config.install_id_path) == "foo-bar-baz"
|
2016-07-11 19:54:34 +02:00
|
|
|
|
2016-10-14 00:45:41 +02:00
|
|
|
|
2022-02-21 18:30:09 -05:00
|
|
|
def test_default_api_url() -> None:
|
2021-10-30 13:21:45 -04:00
|
|
|
config = create_config("")
|
|
|
|
|
assert config.api_url == "https://asciinema.org"
|
2016-07-11 19:54:34 +02:00
|
|
|
|
2016-10-14 00:45:41 +02:00
|
|
|
|
2022-02-21 18:30:09 -05:00
|
|
|
def test_default_record_stdin() -> None:
|
2021-10-30 13:21:45 -04:00
|
|
|
config = create_config("")
|
|
|
|
|
assert config.record_stdin is False
|
2017-09-22 22:21:35 +02:00
|
|
|
|
|
|
|
|
|
2022-02-21 18:30:09 -05:00
|
|
|
def test_default_record_command() -> None:
|
2021-10-30 13:21:45 -04:00
|
|
|
config = create_config("")
|
|
|
|
|
assert config.record_command is None
|
2016-07-11 19:54:34 +02:00
|
|
|
|
2016-10-14 00:45:41 +02:00
|
|
|
|
2022-02-21 18:30:09 -05:00
|
|
|
def test_default_record_env() -> None:
|
2021-10-30 13:21:45 -04:00
|
|
|
config = create_config("")
|
|
|
|
|
assert config.record_env == "SHELL,TERM"
|
2017-09-16 22:18:24 +02:00
|
|
|
|
|
|
|
|
|
2022-02-21 18:30:09 -05:00
|
|
|
def test_default_record_idle_time_limit() -> None:
|
2021-10-30 13:21:45 -04:00
|
|
|
config = create_config("")
|
|
|
|
|
assert config.record_idle_time_limit is None
|
2016-07-11 19:54:34 +02:00
|
|
|
|
2016-10-14 00:45:41 +02:00
|
|
|
|
2022-02-21 18:30:09 -05:00
|
|
|
def test_default_record_yes() -> None:
|
2021-10-30 13:21:45 -04:00
|
|
|
config = create_config("")
|
|
|
|
|
assert config.record_yes is False
|
2016-07-11 19:54:34 +02:00
|
|
|
|
2016-10-14 00:45:41 +02:00
|
|
|
|
2022-02-21 18:30:09 -05:00
|
|
|
def test_default_record_quiet() -> None:
|
2021-10-30 13:21:45 -04:00
|
|
|
config = create_config("")
|
|
|
|
|
assert config.record_quiet is False
|
2016-07-11 19:54:34 +02:00
|
|
|
|
2016-10-14 00:45:41 +02:00
|
|
|
|
2022-02-21 18:30:09 -05:00
|
|
|
def test_default_play_idle_time_limit() -> None:
|
2021-10-30 13:21:45 -04:00
|
|
|
config = create_config("")
|
|
|
|
|
assert config.play_idle_time_limit is None
|
2016-07-11 19:54:34 +02:00
|
|
|
|
2016-10-14 00:45:41 +02:00
|
|
|
|
2022-02-21 18:30:09 -05:00
|
|
|
def test_api_url() -> None:
|
2016-07-11 19:54:34 +02:00
|
|
|
config = create_config("[api]\nurl = http://the/url")
|
2021-10-30 13:21:45 -04:00
|
|
|
assert config.api_url == "http://the/url"
|
2016-07-11 19:54:34 +02:00
|
|
|
|
2016-10-14 00:45:41 +02:00
|
|
|
|
2022-02-21 18:30:09 -05:00
|
|
|
def test_api_url_when_override_set() -> None:
|
2021-10-30 13:21:45 -04:00
|
|
|
config = create_config(
|
|
|
|
|
"[api]\nurl = http://the/url", {"ASCIINEMA_API_URL": "http://the/url2"}
|
|
|
|
|
)
|
|
|
|
|
assert config.api_url == "http://the/url2"
|
2016-07-11 19:54:34 +02:00
|
|
|
|
2016-10-14 00:45:41 +02:00
|
|
|
|
2022-02-21 18:30:09 -05:00
|
|
|
def test_record_command() -> None:
|
2021-10-30 13:21:45 -04:00
|
|
|
command = "bash -l"
|
2022-02-21 18:30:09 -05:00
|
|
|
config = create_config(f"[record]\ncommand = {command}")
|
2021-10-30 13:21:45 -04:00
|
|
|
assert config.record_command == command
|
2016-07-11 19:54:34 +02:00
|
|
|
|
2016-10-14 00:45:41 +02:00
|
|
|
|
2022-02-21 18:30:09 -05:00
|
|
|
def test_record_stdin() -> None:
|
2017-09-22 22:21:35 +02:00
|
|
|
config = create_config("[record]\nstdin = yes")
|
2021-10-30 13:21:45 -04:00
|
|
|
assert config.record_stdin is True
|
2017-09-22 22:21:35 +02:00
|
|
|
|
|
|
|
|
|
2022-02-21 18:30:09 -05:00
|
|
|
def test_record_env() -> None:
|
2017-09-16 22:18:24 +02:00
|
|
|
config = create_config("[record]\nenv = FOO,BAR")
|
2021-10-30 13:21:45 -04:00
|
|
|
assert config.record_env == "FOO,BAR"
|
2017-09-16 22:18:24 +02:00
|
|
|
|
|
|
|
|
|
2022-02-21 18:30:09 -05:00
|
|
|
def test_record_idle_time_limit() -> None:
|
2017-10-26 13:21:38 +02:00
|
|
|
config = create_config("[record]\nidle_time_limit = 2.35")
|
2021-10-30 13:21:45 -04:00
|
|
|
assert config.record_idle_time_limit == 2.35
|
2017-10-26 13:21:38 +02:00
|
|
|
|
|
|
|
|
config = create_config("[record]\nmaxwait = 2.35")
|
2021-10-30 13:21:45 -04:00
|
|
|
assert config.record_idle_time_limit == 2.35
|
2016-07-11 19:54:34 +02:00
|
|
|
|
2016-10-14 00:45:41 +02:00
|
|
|
|
2022-02-21 18:30:09 -05:00
|
|
|
def test_record_yes() -> None:
|
2021-10-30 13:21:45 -04:00
|
|
|
yes = "yes"
|
2022-02-21 18:30:09 -05:00
|
|
|
config = create_config(f"[record]\nyes = {yes}")
|
2021-10-30 13:21:45 -04:00
|
|
|
assert config.record_yes is True
|
2016-07-11 19:54:34 +02:00
|
|
|
|
2016-10-14 00:45:41 +02:00
|
|
|
|
2022-02-21 18:30:09 -05:00
|
|
|
def test_record_quiet() -> None:
|
2021-10-30 13:21:45 -04:00
|
|
|
quiet = "yes"
|
2022-02-21 18:30:09 -05:00
|
|
|
config = create_config(f"[record]\nquiet = {quiet}")
|
2021-10-30 13:21:45 -04:00
|
|
|
assert config.record_quiet is True
|
2016-07-11 19:54:34 +02:00
|
|
|
|
2016-10-14 00:45:41 +02:00
|
|
|
|
2022-02-21 18:30:09 -05:00
|
|
|
def test_play_idle_time_limit() -> None:
|
2017-10-26 13:21:38 +02:00
|
|
|
config = create_config("[play]\nidle_time_limit = 2.35")
|
2021-10-30 13:21:45 -04:00
|
|
|
assert config.play_idle_time_limit == 2.35
|
2017-10-26 13:21:38 +02:00
|
|
|
|
|
|
|
|
config = create_config("[play]\nmaxwait = 2.35")
|
2021-10-30 13:21:45 -04:00
|
|
|
assert config.play_idle_time_limit == 2.35
|
2019-03-31 18:38:00 +02:00
|
|
|
|
|
|
|
|
|
2022-02-21 18:30:09 -05:00
|
|
|
def test_notifications_enabled() -> None:
|
2021-10-30 13:21:45 -04:00
|
|
|
config = create_config("")
|
|
|
|
|
assert config.notifications_enabled is True
|
2019-03-31 18:38:00 +02:00
|
|
|
|
|
|
|
|
config = create_config("[notifications]\nenabled = yes")
|
2021-10-30 13:21:45 -04:00
|
|
|
assert config.notifications_enabled is True
|
2019-03-31 18:38:00 +02:00
|
|
|
|
|
|
|
|
config = create_config("[notifications]\nenabled = no")
|
2021-10-30 13:21:45 -04:00
|
|
|
assert config.notifications_enabled is False
|
2019-03-31 18:38:00 +02:00
|
|
|
|
|
|
|
|
|
2022-02-21 18:30:09 -05:00
|
|
|
def test_notifications_command() -> None:
|
2021-10-30 13:21:45 -04:00
|
|
|
config = create_config("")
|
|
|
|
|
assert config.notifications_command is None
|
2019-03-31 18:38:00 +02:00
|
|
|
|
2021-10-30 13:21:45 -04:00
|
|
|
config = create_config(
|
|
|
|
|
'[notifications]\ncommand = tmux display-message "$TEXT"'
|
|
|
|
|
)
|
|
|
|
|
assert config.notifications_command == 'tmux display-message "$TEXT"'
|