126 lines
4.2 KiB
Ruby
126 lines
4.2 KiB
Ruby
require_relative "test_helper"
|
|
|
|
class ConfigTest < Minitest::Test
|
|
def test_nil_contents_is_unconfigured
|
|
config = Backlog::Config.new(nil)
|
|
refute config.configured?
|
|
assert_nil config.board
|
|
end
|
|
|
|
def test_blank_contents_is_unconfigured
|
|
config = Backlog::Config.new(" \n")
|
|
refute config.configured?
|
|
end
|
|
|
|
def test_reads_board_key
|
|
config = Backlog::Config.new("board=llf-schema\n")
|
|
assert config.configured?
|
|
assert_equal "llf-schema", config.board
|
|
end
|
|
|
|
def test_reads_legacy_yaml_style_board_key
|
|
config = Backlog::Config.new("---\nboard: llf-schema\n")
|
|
assert config.configured?
|
|
assert_equal "llf-schema", config.board
|
|
end
|
|
|
|
def test_lines_without_separator_are_ignored
|
|
config = Backlog::Config.new("# comment\njunk line\n")
|
|
refute config.configured?
|
|
end
|
|
|
|
def test_contents_without_board_key_is_unconfigured
|
|
config = Backlog::Config.new("other=value\n")
|
|
refute config.configured?
|
|
end
|
|
|
|
def test_reads_tracker_key
|
|
config = Backlog::Config.new("tracker=forgejo:jared/cc-os\n")
|
|
assert config.tracker_configured?
|
|
assert_equal "forgejo:jared/cc-os", config.tracker
|
|
end
|
|
|
|
def test_tracker_unconfigured_when_absent
|
|
config = Backlog::Config.new("board=llf-schema\n")
|
|
refute config.tracker_configured?
|
|
end
|
|
|
|
def test_merge_sets_new_key_on_nil_contents
|
|
contents = Backlog::Config.merge(nil, "tracker", "forgejo:jared/cc-os")
|
|
assert_equal "tracker=forgejo:jared/cc-os\n", contents
|
|
end
|
|
|
|
def test_merge_preserves_other_keys
|
|
existing = "board=llf-schema\nother=kept\n"
|
|
updated_contents = Backlog::Config.merge(existing, "tracker", "planka:llf-schema")
|
|
updated = Backlog::Config.new(updated_contents)
|
|
|
|
assert_equal "llf-schema", updated.board
|
|
assert_equal "planka:llf-schema", updated.tracker
|
|
assert_equal "kept", updated.to_h["other"]
|
|
end
|
|
|
|
def test_merge_overwrites_existing_value_for_key
|
|
existing = "tracker=repo:old\n"
|
|
updated = Backlog::Config.new(Backlog::Config.merge(existing, "tracker", "repo:new"))
|
|
assert_equal "repo:new", updated.tracker
|
|
end
|
|
|
|
def test_reads_spaced_key_value_lines
|
|
config = Backlog::Config.new("version = 1\nhub = my-hub\n")
|
|
assert_equal "1", config.to_h["version"]
|
|
assert_equal "my-hub", config.to_h["hub"]
|
|
end
|
|
|
|
def test_merge_output_has_no_spaces_around_equals
|
|
contents = Backlog::Config.merge(nil, "tracker", "forgejo:jared/cc-os")
|
|
assert_equal "tracker=forgejo:jared/cc-os\n", contents
|
|
refute_includes contents, " = "
|
|
refute_includes contents, "= "
|
|
refute_includes contents, " ="
|
|
end
|
|
|
|
def test_merge_normalizes_existing_spaced_lines_on_rewrite
|
|
existing = "version = 1\nhub = my-hub\n"
|
|
updated_contents = Backlog::Config.merge(existing, "tracker", "planka:board")
|
|
updated_contents.each_line do |line|
|
|
next if line.strip.empty?
|
|
|
|
refute_match(/\s=\s|\s=|=\s/, line, "expected normalized key=value, got #{line.inspect}")
|
|
end
|
|
updated = Backlog::Config.new(updated_contents)
|
|
assert_equal "1", updated.to_h["version"]
|
|
assert_equal "my-hub", updated.to_h["hub"]
|
|
assert_equal "planka:board", updated.tracker
|
|
end
|
|
|
|
def test_board_id_reader
|
|
config = Backlog::Config.new("tracker=planka:board\nboard_id=42\n")
|
|
assert_equal "42", config.board_id
|
|
assert_nil Backlog::Config.new(nil).board_id
|
|
end
|
|
|
|
def test_set_preserves_comments_and_blank_lines
|
|
existing = "# managed by cc-os\ntracker=planka:board\n\n# stamped by os-status\nversion=3\n"
|
|
updated = Backlog::Config.set(existing, "board_id", "42")
|
|
assert_equal "# managed by cc-os\ntracker=planka:board\n\n# stamped by os-status\nversion=3\nboard_id=42\n",
|
|
updated
|
|
end
|
|
|
|
def test_set_replaces_existing_key_in_place
|
|
existing = "# top comment\nboard_id=old\ntracker=planka:board\n"
|
|
updated = Backlog::Config.set(existing, "board_id", "new")
|
|
assert_equal "# top comment\nboard_id=new\ntracker=planka:board\n", updated
|
|
end
|
|
|
|
def test_set_normalizes_spaced_key_value_lines_only
|
|
existing = "# note\nversion = 1\n"
|
|
updated = Backlog::Config.set(existing, "board_id", "42")
|
|
assert_equal "# note\nversion=1\nboard_id=42\n", updated
|
|
end
|
|
|
|
def test_set_on_nil_contents
|
|
assert_equal "board_id=42\n", Backlog::Config.set(nil, "board_id", "42")
|
|
end
|
|
end
|