|  | 
|  | 1 | +from __future__ import annotations | 
|  | 2 | + | 
|  | 3 | +from pathlib import Path | 
|  | 4 | +from textwrap import dedent | 
|  | 5 | + | 
|  | 6 | +import pytest | 
|  | 7 | + | 
|  | 8 | +from commitizen.config.base_config import BaseConfig | 
|  | 9 | +from commitizen.providers import get_provider | 
|  | 10 | +from commitizen.providers.dart_provider import DartProvider | 
|  | 11 | + | 
|  | 12 | +DART_YAML = """\ | 
|  | 13 | +name: my_super_package | 
|  | 14 | +description: A new Flutter project. | 
|  | 15 | +
 | 
|  | 16 | +# Bla, bla, bla | 
|  | 17 | +publish_to: "none" # Remove this line if you wish to publish to pub.dev | 
|  | 18 | +
 | 
|  | 19 | +version: 0.1.0+1 | 
|  | 20 | +
 | 
|  | 21 | +environment: | 
|  | 22 | +  sdk: ^3.7.2 | 
|  | 23 | +  # note that this only enforces that the Flutter version is at least the value specified. It does not | 
|  | 24 | +  # force it to this specific version | 
|  | 25 | +  flutter: 3.29.2 | 
|  | 26 | +
 | 
|  | 27 | +# Bla, bla, bla | 
|  | 28 | +dependencies: | 
|  | 29 | +  flutter: | 
|  | 30 | +    sdk: flutter | 
|  | 31 | +
 | 
|  | 32 | +  # Some package dependencies | 
|  | 33 | +  # network | 
|  | 34 | +  dio: ^5.8.0+1 | 
|  | 35 | +
 | 
|  | 36 | +# The following section is specific to Flutter packages. | 
|  | 37 | +flutter: | 
|  | 38 | +  # The following line ensures that the Material Icons font is | 
|  | 39 | +  # included with your application, so that you can use the icons in | 
|  | 40 | +  # the material Icons class. | 
|  | 41 | +  uses-material-design: true | 
|  | 42 | +
 | 
|  | 43 | +  # To add assets to your application, add an assets section, like this: | 
|  | 44 | +  assets: | 
|  | 45 | +    - assets/images/ | 
|  | 46 | +""" | 
|  | 47 | + | 
|  | 48 | +DART_YAML_FLOAT_PARSEABLE_EXPECTED = """\ | 
|  | 49 | +name: my_super_package | 
|  | 50 | +description: A new Flutter project. | 
|  | 51 | +
 | 
|  | 52 | +# Bla, bla, bla | 
|  | 53 | +publish_to: "none" # Remove this line if you wish to publish to pub.dev | 
|  | 54 | +
 | 
|  | 55 | +version: 42.1 | 
|  | 56 | +
 | 
|  | 57 | +environment: | 
|  | 58 | +  sdk: ^3.7.2 | 
|  | 59 | +  # note that this only enforces that the Flutter version is at least the value specified. It does not | 
|  | 60 | +  # force it to this specific version | 
|  | 61 | +  flutter: 3.29.2 | 
|  | 62 | +
 | 
|  | 63 | +# Bla, bla, bla | 
|  | 64 | +dependencies: | 
|  | 65 | +  flutter: | 
|  | 66 | +    sdk: flutter | 
|  | 67 | +
 | 
|  | 68 | +  # Some package dependencies | 
|  | 69 | +  # network | 
|  | 70 | +  dio: ^5.8.0+1 | 
|  | 71 | +
 | 
|  | 72 | +# The following section is specific to Flutter packages. | 
|  | 73 | +flutter: | 
|  | 74 | +  # The following line ensures that the Material Icons font is | 
|  | 75 | +  # included with your application, so that you can use the icons in | 
|  | 76 | +  # the material Icons class. | 
|  | 77 | +  uses-material-design: true | 
|  | 78 | +
 | 
|  | 79 | +  # To add assets to your application, add an assets section, like this: | 
|  | 80 | +  assets: | 
|  | 81 | +    - assets/images/ | 
|  | 82 | +""" | 
|  | 83 | + | 
|  | 84 | +DART_YAML_SEMVER_EXPECTED = """\ | 
|  | 85 | +name: my_super_package | 
|  | 86 | +description: A new Flutter project. | 
|  | 87 | +
 | 
|  | 88 | +# Bla, bla, bla | 
|  | 89 | +publish_to: "none" # Remove this line if you wish to publish to pub.dev | 
|  | 90 | +
 | 
|  | 91 | +version: 2.3.4 | 
|  | 92 | +
 | 
|  | 93 | +environment: | 
|  | 94 | +  sdk: ^3.7.2 | 
|  | 95 | +  # note that this only enforces that the Flutter version is at least the value specified. It does not | 
|  | 96 | +  # force it to this specific version | 
|  | 97 | +  flutter: 3.29.2 | 
|  | 98 | +
 | 
|  | 99 | +# Bla, bla, bla | 
|  | 100 | +dependencies: | 
|  | 101 | +  flutter: | 
|  | 102 | +    sdk: flutter | 
|  | 103 | +
 | 
|  | 104 | +  # Some package dependencies | 
|  | 105 | +  # network | 
|  | 106 | +  dio: ^5.8.0+1 | 
|  | 107 | +
 | 
|  | 108 | +# The following section is specific to Flutter packages. | 
|  | 109 | +flutter: | 
|  | 110 | +  # The following line ensures that the Material Icons font is | 
|  | 111 | +  # included with your application, so that you can use the icons in | 
|  | 112 | +  # the material Icons class. | 
|  | 113 | +  uses-material-design: true | 
|  | 114 | +
 | 
|  | 115 | +  # To add assets to your application, add an assets section, like this: | 
|  | 116 | +  assets: | 
|  | 117 | +    - assets/images/ | 
|  | 118 | +""" | 
|  | 119 | + | 
|  | 120 | + | 
|  | 121 | +@pytest.mark.parametrize( | 
|  | 122 | +    "content, expected", | 
|  | 123 | +    ((DART_YAML, DART_YAML_FLOAT_PARSEABLE_EXPECTED),), | 
|  | 124 | +) | 
|  | 125 | +def test_dart_provider_with_float_parseable_new_version( | 
|  | 126 | +    config: BaseConfig, | 
|  | 127 | +    chdir: Path, | 
|  | 128 | +    content: str, | 
|  | 129 | +    expected: str, | 
|  | 130 | +): | 
|  | 131 | +    filename = DartProvider.package_filename | 
|  | 132 | +    file = chdir / filename | 
|  | 133 | +    file.write_text(dedent(content)) | 
|  | 134 | +    config.settings["version_provider"] = "dart" | 
|  | 135 | + | 
|  | 136 | +    provider = get_provider(config) | 
|  | 137 | +    assert isinstance(provider, DartProvider) | 
|  | 138 | +    assert provider.get_version() == "0.1.0" | 
|  | 139 | + | 
|  | 140 | +    provider.set_version("42.1") | 
|  | 141 | +    assert file.read_text() == dedent(expected) | 
|  | 142 | + | 
|  | 143 | + | 
|  | 144 | +@pytest.mark.parametrize( | 
|  | 145 | +    "content, expected", | 
|  | 146 | +    ((DART_YAML, DART_YAML_SEMVER_EXPECTED),), | 
|  | 147 | +) | 
|  | 148 | +def test_dart_provider_with_semver_new_version( | 
|  | 149 | +    config: BaseConfig, | 
|  | 150 | +    chdir: Path, | 
|  | 151 | +    content: str, | 
|  | 152 | +    expected: str, | 
|  | 153 | +): | 
|  | 154 | +    filename = DartProvider.package_filename | 
|  | 155 | +    file = chdir / filename | 
|  | 156 | +    file.write_text(dedent(content)) | 
|  | 157 | +    config.settings["version_provider"] = "dart" | 
|  | 158 | + | 
|  | 159 | +    provider = get_provider(config) | 
|  | 160 | +    assert isinstance(provider, DartProvider) | 
|  | 161 | +    assert provider.get_version() == "0.1.0" | 
|  | 162 | + | 
|  | 163 | +    provider.set_version("2.3.4") | 
|  | 164 | +    assert file.read_text() == dedent(expected) | 
0 commit comments