"""Unit tests for utils.github.run_name: the emoji head each input's segment opens with, the openings that terminate a value, and the round trip through the declared run name.""" from __future__ import annotations import unittest from tests.utils.ci.run_name import render from utils.github import run_name TPL = ( "๐Ÿ•น๏ธ " "${{ inputs.sequencing != 'auto'" " && format('๐Ÿ›{0} ', inputs.sequencing == 'serial' && 'ๅบ' || 'ๅนถ') || '' }}" "${{ inputs.mode_fail_fast && '๐Ÿ›‘ ' || '' }}" "${{ inputs.workspace != 'auto'" " && format('๐Ÿง‘{0} ', inputs.workspace == 'true' && 'ๆ˜ฏ' || 'ๅฆ') || '' }}" "${{ inputs.distros != '' && format('๐Ÿง{0} ', inputs.distros) || '' }}" "${{ inputs.priority != '' && format('โญ{0} ', inputs.priority) || '' }}" "${{ inputs.whitelist != ''" " && format('๐ŸŽฏ{0}', inputs.whitelist) || '๐Ÿ”€ diff (origin/main)' }}" ) class SegmentTests(unittest.TestCase): def test_value_segments_map_their_input_to_a_head(self) -> None: self.assertEqual( run_name.heads(TPL), { "sequencing": "๐Ÿ›", "workspace": "๐Ÿง‘", "distros": "๐Ÿง", "priority": "โญ", "whitelist": "๐ŸŽฏ", }, ) def test_a_valueless_segment_is_a_marker(self) -> None: self.assertEqual(run_name.markers(TPL), {"mode_fail_fast": "๐Ÿ›‘"}) def test_an_input_without_a_value_segment_is_rejected(self) -> None: with self.assertRaises(ValueError): run_name.value_from_title("๐Ÿ•น๏ธ ๐Ÿงarch", "lifecycles", TPL) class HeadlessSegmentTests(unittest.TestCase): """An input whose ``format()`` opens with nothing carries no value. ``str.find('')`` matches at position 0, so an empty head would hand back the title's own prefix as the value. Such an input renders a glyph for the reader and is recovered from the job log instead.""" NETWORK = ( "${{ inputs.network != 'auto' && format('{0} '," " inputs.network == 'clearnet' && '๐ŸŒ'" " || inputs.network == 'tor' && '๐Ÿง…' || '๐ŸŒˆ') || '' }}" ) TPL = ( "๐Ÿ•น๏ธ " + NETWORK + "${{ inputs.distros != '' && format('๐Ÿง{0} ', inputs.distros) || '' }}" ) def test_it_is_the_network_segment_the_declared_run_name_renders(self) -> None: self.assertIn(self.NETWORK, run_name.template()) def test_it_yields_no_segment(self) -> None: self.assertNotIn("network", run_name.heads(self.TPL)) self.assertNotIn("network", run_name.markers(self.TPL)) def test_its_glyphs_still_terminate_the_value_before_them(self) -> None: found = run_name.openings(self.TPL) self.assertIn("๐ŸŒ", found) self.assertIn("๐Ÿง…", found) self.assertIn("๐ŸŒˆ", found) def test_a_neighbour_value_survives_the_glyph(self) -> None: self.assertEqual( run_name.value_from_title("๐Ÿ•น๏ธ ๐ŸŒˆ ๐Ÿงarch debian", "distros", self.TPL), "arch debian", ) def test_the_title_reports_no_value_for_it(self) -> None: self.assertNotIn("network", run_name.values_from_title("๐Ÿ•น๏ธ ๐Ÿง… ๐Ÿงarch", self.TPL)) class OpeningTests(unittest.TestCase): def test_compared_operands_and_value_glyphs_are_not_openings(self) -> None: found = run_name.openings(TPL) self.assertNotIn("auto", found) self.assertNotIn("true", found) self.assertNotIn("ๆ˜ฏ", found) self.assertNotIn("ๅฆ", found) self.assertNotIn("ๅบ", found) def test_the_diff_fallback_is_an_opening(self) -> None: self.assertIn("๐Ÿ”€ diff (origin/main)", run_name.openings(TPL)) class ValueTests(unittest.TestCase): def test_a_following_segment_ends_the_value(self) -> None: title = "๐Ÿ•น๏ธ ๐Ÿงdebian ubuntu โญweb-app-x ๐ŸŽฏ__ALL__" self.assertEqual( run_name.value_from_title(title, "distros", TPL), "debian ubuntu" ) def test_the_diff_fallback_ends_the_value(self) -> None: title = "๐Ÿ•น๏ธ ๐Ÿงarch ๐Ÿ”€ diff (origin/main)" self.assertEqual(run_name.value_from_title(title, "distros", TPL), "arch") def test_a_glyph_reads_back_as_its_value(self) -> None: title = "๐Ÿ•น๏ธ ๐Ÿง‘ๆ˜ฏ ๐Ÿงarch" self.assertEqual(run_name.value_from_title(title, "workspace", TPL), "true") def test_a_marker_reads_back_by_its_presence(self) -> None: self.assertEqual( run_name.values_from_title("๐Ÿ•น๏ธ ๐Ÿ›‘ ๐Ÿงarch", TPL)["mode_fail_fast"], "true" ) self.assertEqual( run_name.values_from_title("๐Ÿ•น๏ธ ๐Ÿงarch", TPL)["mode_fail_fast"], "false" ) def test_a_default_input_records_nothing(self) -> None: title = "๐Ÿ•น๏ธ ๐Ÿงarch ๐Ÿ”€ diff (origin/main)" self.assertEqual(run_name.value_from_title(title, "sequencing", TPL), "") def test_a_foreign_title_yields_nothing(self) -> None: self.assertEqual(run_name.values_from_title("CI: Pull Request", TPL), {}) class DeclaredTemplateTests(unittest.TestCase): def test_the_declared_run_name_round_trips(self) -> None: dispatched = { "chunk_gate": "true", "workspace": "true", "filesystem": "ext4", "distros": "debian arch", "lifecycles": "stable", "mode": "swarm", } self.assertEqual(run_name.values_from_title(render(dispatched)), dispatched) if __name__ == "__main__": unittest.main()