#!/usr/bin/env python # License: GPL v3 Copyright: 2026, Kovid Goyal import os import shutil import tempfile from unittest.mock import patch from kitty.config import load_config from kitty.utils import log_error from .base import BaseTest class TestConfigProfiles(BaseTest): def setUp(self): super().setUp() self.error_messages: list[str] = [] log_error.redirect = self.error_messages.append self.tdir = tempfile.mkdtemp() self.conf = os.path.join(self.tdir, 'kitty.conf') def tearDown(self): del log_error.redirect shutil.rmtree(self.tdir) super().tearDown() def write_conf(self, text: str) -> str: with open(self.conf, 'w', encoding='utf-8') as f: f.write(text) return self.conf def test_profile_overrides_only_the_options_it_declares(self): self.write_conf('font_size 13\n\nprofile work {\n font_size 9\n cursor_shape beam\n}\n') base = load_config(self.conf) self.ae(base.font_size, 13) self.ae(base.profile_name, '') work = load_config(self.conf, profile='work') self.ae(work.profile_name, 'work') self.ae(work.font_size, 9) self.assertNotEqual(work.cursor_shape, base.cursor_shape) # Options absent from the profile keep the global value self.ae(work.background_opacity, base.background_opacity) def test_profile_is_selected_by_environment_variable(self): self.write_conf('font_size 13\nprofile dark {\n font_size 21\n}\n') with patch.dict(os.environ, {'KITTY_PROFILE': 'dark'}): self.ae(load_config(self.conf).font_size, 21) # An explicitly requested profile wins over the environment with patch.dict(os.environ, {'KITTY_PROFILE': 'dark'}): self.ae(load_config(self.conf, profile='').font_size, 21) self.write_conf('font_size 13\nprofile dark {\n font_size 21\n}\nprofile light {\n font_size 17\n}\n') self.ae(load_config(self.conf, profile='light').font_size, 17) def test_unknown_profile_is_reported_without_blocking_startup(self): self.write_conf('font_size 13\n') opts = load_config(self.conf, profile='missing') self.ae(opts.font_size, 13) self.ae(opts.profile_name, 'missing') self.assertTrue(any('missing' in msg for msg in self.error_messages), self.error_messages) def test_invalid_line_in_profile_does_not_block_startup(self): self.write_conf('font_size 12\nprofile work {\n font_size not-a-number\n background_opacity 0.5\n}\n') bad_lines = [] opts = load_config(self.conf, profile='work', accumulate_bad_lines=bad_lines) self.ae(opts.font_size, 12) self.ae(opts.background_opacity, 0.5) self.ae(len(bad_lines), 1) self.assertIn('font_size not-a-number', bad_lines[0].line) self.ae(bad_lines[0].file, self.conf) def test_unterminated_profile_is_reported(self): self.write_conf('profile work {\n font_size 9\n') bad_lines = [] opts = load_config(self.conf, accumulate_bad_lines=bad_lines) self.ae(len(bad_lines), 1) self.assertIn('never closed', str(bad_lines[0].exception)) self.assertNotEqual(opts.font_size, 9) def test_profile_in_included_file(self): with open(os.path.join(self.tdir, 'extra.conf'), 'w', encoding='utf-8') as f: f.write('profile extra {\n font_size 30\n}\n') self.write_conf('include extra.conf\nfont_size 13\n') self.ae(load_config(self.conf, profile='extra').font_size, 30) self.ae(load_config(self.conf).font_size, 13) def test_mappings_from_profile_are_added(self): self.write_conf('map ctrl+a new_tab\n\nprofile work {\n map ctrl+b new_window\n}\n') base = load_config(self.conf) work = load_config(self.conf, profile='work') self.ae(len(work.keyboard_modes[''].keymap), len(base.keyboard_modes[''].keymap) + 1) def test_profile_name_on_command_line(self): from kitty.cli import parse_args args, rest = parse_args(['--profile', 'work']) self.ae(args.profile, 'work') self.ae(rest, [])