chromium/tools/crates/create_update_cl_unittests.py

#!/usr/bin/env vpython3

# Copyright 2024 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import os
import unittest

from create_update_cl import (
    CHROMIUM_DIR,
    ConvertCrateIdToCrateName,
    ConvertCrateIdToCrateVersion,
    ConvertCrateIdToEpochDir,
    ConvertCrateIdToGnLabel,
    ConvertCrateIdToVendorDir,
    CreateCommitDescription,
    CreateCommitTitle,
    DiffCrateIds,
    GetEpoch,
    SortedMarkdownList,
)

class DiffCrateIdsTests(unittest.TestCase):

    def testBasicsViaMinorUpdateDetection(self):
        before = set(
            ["[email protected]", "[email protected]", "[email protected]", "[email protected]"])
        after = set(
            ["[email protected]", "[email protected]", "[email protected]", "[email protected]"])
        diff = DiffCrateIds(before, after, only_minor_updates=True)
        self.assertEqual(diff.added_crate_ids, ["[email protected]"])
        self.assertEqual(diff.removed_crate_ids, ["[email protected]"])
        self.assertEqual(len(diff.updates), 2)
        self.assertEqual(diff.updates[0].old_crate_id, "[email protected]")
        self.assertEqual(diff.updates[0].new_crate_id, "[email protected]")
        self.assertEqual(diff.updates[1].old_crate_id, "[email protected]")
        self.assertEqual(diff.updates[1].new_crate_id, "[email protected]")

    def testMajorUpdateDetection(self):
        before = set(["[email protected]"])
        after = set(["[email protected]"])
        diff = DiffCrateIds(before, after, only_minor_updates=False)
        self.assertEqual(diff.added_crate_ids, [])
        self.assertEqual(diff.removed_crate_ids, [])
        self.assertEqual(len(diff.updates), 1)
        self.assertEqual(diff.updates[0].old_crate_id, "[email protected]")
        self.assertEqual(diff.updates[0].new_crate_id, "[email protected]")

    def testMajorUpdateWithOnlyMinorUpdateDetection(self):
        before = set(["[email protected]"])
        after = set(["[email protected]"])
        diff = DiffCrateIds(before, after, only_minor_updates=True)
        self.assertEqual(diff.added_crate_ids, ["[email protected]"])
        self.assertEqual(diff.removed_crate_ids, ["[email protected]"])
        self.assertEqual(diff.updates, [])

    def testNoChanges(self):
        before = set(["[email protected]"])
        after = set(["[email protected]"])
        diff = DiffCrateIds(before, after, only_minor_updates=True)
        self.assertEqual(diff.added_crate_ids, [])
        self.assertEqual(diff.removed_crate_ids, [])
        self.assertEqual(diff.updates, [])


class CommitDescriptionTests(unittest.TestCase):

    def testTitle(self):
        before = set(["[email protected]", "[email protected]"])
        after = set(["[email protected]", "[email protected]"])
        diff = DiffCrateIds(before, after, only_minor_updates=True)
        actual_title = CreateCommitTitle("[email protected]", diff)
        expected_title = \
             "Roll updated_crate: 2.0.1 => 2.0.2 in //third_party/rust."
        self.assertEqual(actual_title, expected_title)

    def testFullDescription(self):
        before = set(["[email protected]", "[email protected]"])
        after = set(["[email protected]", "[email protected]"])
        diff = DiffCrateIds(before, after, only_minor_updates=True)

        actual_desc = CreateCommitDescription("Commit title.", diff, False)
        expected_desc = \
"""Commit title.

This CL has been created semi-automatically.  The expected review
process and other details can be found at
//tools/crates/create_update_cl.md

Updated crates:

* updated_crate: 2.0.1 => 2.0.2

New crates:

* [email protected]

Removed crates:

* [email protected]

Bug: None
Cq-Include-Trybots: chromium/try:android-rust-arm32-rel
Cq-Include-Trybots: chromium/try:android-rust-arm64-dbg
Cq-Include-Trybots: chromium/try:android-rust-arm64-rel
Cq-Include-Trybots: chromium/try:linux-rust-x64-dbg
Cq-Include-Trybots: chromium/try:linux-rust-x64-rel
Cq-Include-Trybots: chromium/try:win-rust-x64-dbg
Cq-Include-Trybots: chromium/try:win-rust-x64-rel
Disable-Rts: True
"""
        self.assertEqual(actual_desc, expected_desc)


class OtherTests(unittest.TestCase):

    def testGetEpoch(self):
        self.assertEqual(GetEpoch("0.1.2"), "v0_1")
        self.assertEqual(GetEpoch("1.2.3"), "v1")

    def testConvertCrateIdToEpochDir(self):
        actual_dir = ConvertCrateIdToEpochDir("[email protected]")
        expected_suffix = os.path.join("third_party", "rust", "foo_bar", "v1")
        self.assertEqual(actual_dir, os.path.join(CHROMIUM_DIR,
                                                  expected_suffix))

        actual_dir = ConvertCrateIdToEpochDir("[email protected]")
        expected_suffix = os.path.join("third_party", "rust", "bar_baz",
                                       "v0_12")
        self.assertEqual(actual_dir, os.path.join(CHROMIUM_DIR,
                                                  expected_suffix))

    def testConvertCrateIdToGnLabel(self):
        self.assertEqual(ConvertCrateIdToGnLabel("[email protected]"),
                         "//third_party/rust/foo_bar/v1:lib")
        self.assertEqual(ConvertCrateIdToGnLabel("[email protected]"),
                         "//third_party/rust/bar_baz/v0_12:lib")

    def testConvertCrateIdToVendorDir(self):
        actual_dir = ConvertCrateIdToVendorDir("[email protected]")
        expected_suffix = os.path.join("third_party", "rust",
                                       "chromium_crates_io", "vendor",
                                       "foo-bar-1.2.3")
        self.assertEqual(actual_dir, os.path.join(CHROMIUM_DIR,
                                                  expected_suffix))

        actual_dir = ConvertCrateIdToVendorDir("[email protected]")
        expected_suffix = os.path.join("third_party", "rust",
                                       "chromium_crates_io", "vendor",
                                       "bar_baz-0.12.3")
        self.assertEqual(actual_dir, os.path.join(CHROMIUM_DIR,
                                                  expected_suffix))

    def testConvertCrateIdToCrateName(self):
        self.assertEqual(ConvertCrateIdToCrateName("[email protected]"), "foo-bar")

    def testConvertCrateIdToCrateVersion(self):
        self.assertEqual(ConvertCrateIdToCrateVersion("[email protected]"), "1.2.3")

    def testSortedMarkdownList(self):
        input = ["bbb " * 25, "aaa " * 30, "ccc " * 35]
        actual_output = SortedMarkdownList(input)
        expected_output = \
"""* aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa
  aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa
* bbb bbb bbb bbb bbb bbb bbb bbb bbb bbb bbb bbb bbb bbb bbb bbb bbb
  bbb bbb bbb bbb bbb bbb bbb bbb
* ccc ccc ccc ccc ccc ccc ccc ccc ccc ccc ccc ccc ccc ccc ccc ccc ccc
  ccc ccc ccc ccc ccc ccc ccc ccc ccc ccc ccc ccc ccc ccc ccc ccc ccc
  ccc"""
        self.assertEqual(actual_output, expected_output)


if __name__ == '__main__':
    unittest.main()