github.com/containers/podman/v4@v4.9.4/hack/markdown-preprocess.t (about)

     1  #!/usr/bin/env python3
     2  
     3  """
     4  Tests for markdown-preprocess
     5  """
     6  
     7  import unittest
     8  
     9  # https://stackoverflow.com/questions/66665217/how-to-import-a-python-script-without-a-py-extension
    10  from importlib.util import spec_from_loader, module_from_spec
    11  from importlib.machinery import SourceFileLoader
    12  
    13  spec = spec_from_loader("mp", SourceFileLoader("mp", "hack/markdown-preprocess"))
    14  mp = module_from_spec(spec)
    15  spec.loader.exec_module(mp)
    16  
    17  pp = mp.Preprocessor()
    18  
    19  class TestPodReplacer(unittest.TestCase):
    20      def check_4_way(self, containerstring: str, podstring: str):
    21          types = ['container', 'pod']
    22          strings = [ containerstring, podstring ]
    23          for i in 0, 1:
    24              pp.pod_or_container = types[i]
    25              for j in 0, 1:
    26                  s = '<<' + strings[j] + '|' + strings[(j+1)%2] + '>>'
    27                  self.assertEqual(pp.replace_type(s), strings[i])
    28  
    29      def test_basic(self):
    30          """basic pod|container and vice-versa"""
    31          self.check_4_way('container', 'pod')
    32  
    33      def test_case_insensitive(self):
    34          """test case-insensitive replacement of Pod, Container"""
    35          self.check_4_way('Container', 'Pod')
    36  
    37      def test_dont_care_about_podman(self):
    38          """we ignore 'podman'"""
    39          self.check_4_way('podman container', 'pod in podman')
    40  
    41      def test_not_at_beginning(self):
    42          """oops - test for 'pod' other than at beginning of string"""
    43          self.check_4_way('container', 'container or pod')
    44  
    45      def test_blank(self):
    46          """test that either side of '|' can be empty"""
    47          s_lblank = 'abc container<<| or pod>> def'
    48          s_rblank = 'abc container<< or pod|>> def'
    49  
    50          pp.pod_or_container = 'container'
    51          self.assertEqual(pp.replace_type(s_lblank), 'abc container def')
    52          self.assertEqual(pp.replace_type(s_rblank), 'abc container def')
    53  
    54          pp.pod_or_container = 'pod'
    55          self.assertEqual(pp.replace_type(s_lblank), 'abc container or pod def')
    56          self.assertEqual(pp.replace_type(s_rblank), 'abc container or pod def')
    57  
    58      def test_exception_both(self):
    59          """test that 'pod' on both sides raises exception"""
    60          for word in ['pod', 'container']:
    61              pp.pod_or_container = word
    62              with self.assertRaisesRegex(Exception, "in both left and right sides"):
    63                  pp.replace_type('<<pod 123|pod 321>>')
    64  
    65      def test_exception_neither(self):
    66          """test that 'pod' on neither side raises exception"""
    67          for word in ['pod', 'container']:
    68              pp.pod_or_container = word
    69              with self.assertRaisesRegex(Exception, "in either side"):
    70                  pp.replace_type('<<container 123|container 321>>')
    71  
    72  class TestPodmanSubcommand(unittest.TestCase):
    73      def test_basic(self):
    74          """podman subcommand basic test"""
    75          pp.infile = 'podman-foo.1.md.in'
    76          self.assertEqual(pp.podman_subcommand(), "foo")
    77  
    78          pp.infile = 'podman-foo-bar.1.md.in'
    79          self.assertEqual(pp.podman_subcommand(), "foo bar")
    80  
    81          pp.infile = 'podman-pod-rm.1.md.in'
    82          self.assertEqual(pp.podman_subcommand(), "rm")
    83          self.assertEqual(pp.podman_subcommand("full"), "pod rm")
    84  
    85  if __name__ == '__main__':
    86      unittest.main()