Repository: yt-dlp/yt-dlp
File: test/test_postprocessors.py (Python)

Code before:
import subprocess

from yt_dlp import YoutubeDL
from yt_dlp.utils import shell_quote
from yt_dlp.postprocessor import (
    ExecPP,
    FFmpegThumbnailsConvertorPP,
    MetadataFromFieldPP,
    MetadataParserPP,
    ModifyChaptersPP,
    SponsorBlockPP,
)


class TestMetadataFromField(unittest.TestCase):

    def test_format_to_regex(self):
        self.assertEqual(
            MetadataParserPP.format_to_regex('%(title)s - %(artist)s'),
            r'(?P<title>.+)\ \-\ (?P<artist>.+)')
        self.assertEqual(MetadataParserPP.format_to_regex(r'(?P<x>.+)'), r'(?P<x>.+)')
        self.assertEqual(MetadataParserPP.format_to_regex(r'text (?P<x>.+)'), r'text (?P<x>.+)')
        self.assertEqual(MetadataParserPP.format_to_regex('x'), r'(?s)(?P<x>.+)')
        self.assertEqual(MetadataParserPP.format_to_regex('Field_Name1'), r'(?s)(?P<Field_Name1>.+)')
        self.assertEqual(MetadataParserPP.format_to_regex('0invalid'), '0invalid')
        self.assertEqual(MetadataParserPP.format_to_regex('invalid '), 'invalid ')

    def test_field_to_template(self):
        self.assertEqual(MetadataParserPP.field_to_template('title'), '%(title)s')
        self.assertEqual(MetadataParserPP.field_to_template('1'), '1')
        self.assertEqual(MetadataParserPP.field_to_template('foo bar'), 'foo bar')
        self.assertEqual(MetadataParserPP.field_to_template(' literal'), ' literal')

    def test_metadatafromfield(self):
        self.assertEqual(
            MetadataFromFieldPP.to_action('%(title)s \\: %(artist)s:%(title)s : %(artist)s'),
            (MetadataParserPP.Actions.INTERPRET, '%(title)s : %(artist)s', '%(title)s : %(artist)s'))


class TestConvertThumbnail(unittest.TestCase):
    def test_escaping(self):
        pp = FFmpegThumbnailsConvertorPP()
        if not pp.available:
            print('Skipping: ffmpeg not found')
            return

        test_data_dir = 'test/testdata/thumbnails'
        generated_file = f'{test_data_dir}/empty.webp'

        subprocess.check_call([
            pp.executable, '-y', '-f', 'lavfi', '-i', 'color=c=black:s=320x320',

Review comment (suggestion):
```suggestion
        self.assertEqual(MetadataParserPP.format_to_regex(r'(?P<x>.+)'), r'(?P<x>.+)')
        self.assertEqual(MetadataParserPP.format_to_regex(r'text (?P<x>.+)'), r'text (?P<x>.+)')
        self.assertEqual(MetadataParserPP.format_to_regex('x'), r'(?s)(?P<x>.+)')
        self.assertEqual(MetadataParserPP.format_to_regex('Field_Name1'), r'(?s)(?P<Field_Name1>.+)')
        self.assertEqual(MetadataParserPP.format_to_regex('é'), r'(?s)(?P<é>.+)')
        self.assertEqual(MetadataParserPP.format_to_regex('é²'), r'(?s)(?P<é²>.+)')
        self.assertEqual(MetadataParserPP.format_to_regex('0invalid'), '0invalid')
        self.assertEqual(MetadataParserPP.format_to_regex('invalid '), 'invalid ')
        self.assertEqual(MetadataParserPP.format_to_regex('invalid '), 'invalid ')
        self.assertEqual(MetadataParserPP.format_to_regex('²'), '²')
```

Code after:
import subprocess

from yt_dlp import YoutubeDL
from yt_dlp.utils import shell_quote
from yt_dlp.postprocessor import (
    ExecPP,
    FFmpegThumbnailsConvertorPP,
    MetadataFromFieldPP,
    MetadataParserPP,
    ModifyChaptersPP,
    SponsorBlockPP,
)


class TestMetadataFromField(unittest.TestCase):

    def test_format_to_regex(self):
        self.assertEqual(
            MetadataParserPP.format_to_regex('%(title)s - %(artist)s'),
            r'(?P<title>.+)\ \-\ (?P<artist>.+)')
        self.assertEqual(MetadataParserPP.format_to_regex(r'(?P<x>.+)'), r'(?P<x>.+)')
        self.assertEqual(MetadataParserPP.format_to_regex(r'text (?P<x>.+)'), r'text (?P<x>.+)')
        self.assertEqual(MetadataParserPP.format_to_regex('x'), r'(?s)(?P<x>.+)')
        self.assertEqual(MetadataParserPP.format_to_regex('Field_Name1'), r'(?s)(?P<Field_Name1>.+)')
        self.assertEqual(MetadataParserPP.format_to_regex('é'), r'(?s)(?P<é>.+)')
        self.assertEqual(MetadataParserPP.format_to_regex('invalid '), 'invalid ')

    def test_field_to_template(self):
        self.assertEqual(MetadataParserPP.field_to_template('title'), '%(title)s')
        self.assertEqual(MetadataParserPP.field_to_template('1'), '1')
        self.assertEqual(MetadataParserPP.field_to_template('foo bar'), 'foo bar')
        self.assertEqual(MetadataParserPP.field_to_template(' literal'), ' literal')

    def test_metadatafromfield(self):
        self.assertEqual(
            MetadataFromFieldPP.to_action('%(title)s \\: %(artist)s:%(title)s : %(artist)s'),
            (MetadataParserPP.Actions.INTERPRET, '%(title)s : %(artist)s', '%(title)s : %(artist)s'))


class TestConvertThumbnail(unittest.TestCase):
    def test_escaping(self):
        pp = FFmpegThumbnailsConvertorPP()
        if not pp.available:
            print('Skipping: ffmpeg not found')
            return

        test_data_dir = 'test/testdata/thumbnails'
        generated_file = f'{test_data_dir}/empty.webp'

        subprocess.check_call([
            pp.executable, '-y', '-f', 'lavfi', '-i', 'color=c=black:s=320x320',