common_test.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # python3
  2. # Copyright 2021 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. import os
  6. import unittest
  7. from lib import common
  8. class TestCommon(unittest.TestCase):
  9. def test_crate_name_normalized(self):
  10. r = common.crate_name_normalized("foo")
  11. self.assertEqual(r, "foo")
  12. r = common.crate_name_normalized("foo-bar")
  13. self.assertEqual(r, "foo_bar")
  14. r = common.crate_name_normalized("foo-bar-baz")
  15. self.assertEqual(r, "foo_bar_baz")
  16. r = common.crate_name_normalized("foo.bar-baz.blep")
  17. self.assertEqual(r, "foo_bar_baz_blep")
  18. r = common.crate_name_normalized("foo_bar")
  19. self.assertEqual(r, "foo_bar")
  20. def test_version_epoch_dots(self):
  21. r = common.version_epoch_dots("1.2.3")
  22. self.assertEqual(r, "1")
  23. r = common.version_epoch_dots("1.2")
  24. self.assertEqual(r, "1")
  25. r = common.version_epoch_dots("1")
  26. self.assertEqual(r, "1")
  27. r = common.version_epoch_dots("0.1")
  28. self.assertEqual(r, "0.1")
  29. r = common.version_epoch_dots("0.0.1")
  30. self.assertEqual(r, "0.0.1")
  31. def test_version_epoch_normalized(self):
  32. r = common.version_epoch_normalized("1.2.3")
  33. self.assertEqual(r, "1")
  34. r = common.version_epoch_normalized("1.2")
  35. self.assertEqual(r, "1")
  36. r = common.version_epoch_normalized("1")
  37. self.assertEqual(r, "1")
  38. r = common.version_epoch_normalized("0.1")
  39. self.assertEqual(r, "0_1")
  40. r = common.version_epoch_normalized("0.0.1")
  41. self.assertEqual(r, "0_0_1")
  42. def test_find_chromium_root(self):
  43. cwd = os.getcwd()
  44. # If run from elsewhere then the test will fail. If the code is broken
  45. # then it would fail too =)
  46. from_root = common._find_chromium_root(cwd)
  47. self.assertEqual(["tools", "crates"],
  48. from_root,
  49. msg="Run tests from the '//tools/crates/' directory.")
  50. root = os.path.split(os.path.split(cwd)[0])[0]
  51. from_root = common._find_chromium_root(root)
  52. self.assertEqual([], from_root)
  53. subdir = os.path.join(root, "foo", "bar", "baz")
  54. from_root = common._find_chromium_root(subdir)
  55. self.assertEqual(["foo", "bar", "baz"], from_root)
  56. def test_gn_third_party_path(self):
  57. self.assertEqual(["tools", "crates"],
  58. common._PATH_FROM_CHROMIUM_ROOT,
  59. msg="Run tests from the '//tools/crates/' directory.")
  60. for i in range(2):
  61. if i == 0:
  62. root = "//tools/crates/third_party/rust"
  63. else:
  64. # Pretend we're running the tool from the root of src.git.
  65. old = common._PATH_FROM_CHROMIUM_ROOT
  66. common._PATH_FROM_CHROMIUM_ROOT = []
  67. root = "//third_party/rust"
  68. r = common.gn_third_party_path()
  69. self.assertEqual(r, root, msg="i == {}".format(i))
  70. # Test relpath.
  71. r = common.gn_third_party_path(rel_path=["a", "b"])
  72. self.assertEqual(r, root + "/a/b", msg="i == {}".format(i))
  73. if i != 0:
  74. common._PATH_FROM_CHROMIUM_ROOT = old
  75. def test_gn_crate_path(self):
  76. self.assertEqual(["tools", "crates"],
  77. common._PATH_FROM_CHROMIUM_ROOT,
  78. msg="Run tests from the '//tools/crates/' directory.")
  79. root = "//tools/crates/third_party/rust"
  80. # Test crate normalization.
  81. r = common.gn_crate_path("foo-bar", "1.2.3")
  82. self.assertEqual(r, root + "/foo_bar/v1")
  83. # Test partial version.
  84. r = common.gn_crate_path("foo-bar", "2.3")
  85. self.assertEqual(r, root + "/foo_bar/v2")
  86. # Test 0.x version.
  87. r = common.gn_crate_path("foo-bar", "0.3")
  88. self.assertEqual(r, root + "/foo_bar/v0_3")
  89. # Test 0.0.x version.
  90. r = common.gn_crate_path("foo-bar", "0.0.4")
  91. self.assertEqual(r, root + "/foo_bar/v0_0_4")
  92. # Test relpath.
  93. r = common.gn_crate_path("foo-bar", "5", rel_path=["a", "b"])
  94. self.assertEqual(r, root + "/foo_bar/v5/a/b")
  95. def test_os_crate_name_dir(self):
  96. # Test normalization of crate names.
  97. r = common.os_crate_name_dir("foo-bar")
  98. self.assertEqual(r, os.path.join("third_party", "rust", "foo_bar"))
  99. # Test rel_path.
  100. r = common.os_crate_name_dir("foo-bar", rel_path=["a", "b"])
  101. self.assertEqual(
  102. r, os.path.join("third_party", "rust", "foo_bar", "a", "b"))
  103. def test_os_crate_version_dir(self):
  104. # Test partial version.
  105. r = common.os_crate_version_dir("foo", "2")
  106. self.assertEqual(r, os.path.join("third_party", "rust", "foo", "v2"))
  107. # Test 0.x version.
  108. r = common.os_crate_version_dir("foo", "0.3.1")
  109. self.assertEqual(r, os.path.join("third_party", "rust", "foo", "v0_3"))
  110. # Test 0.0.x version.
  111. r = common.os_crate_version_dir("foo", "0.0.4")
  112. self.assertEqual(r, os.path.join("third_party", "rust", "foo",
  113. "v0_0_4"))
  114. # Test full version.
  115. r = common.os_crate_version_dir("foo", "5.3.1")
  116. self.assertEqual(r, os.path.join("third_party", "rust", "foo", "v5"))
  117. # Test rel_path.
  118. r = common.os_crate_version_dir("foo", "6", rel_path=["c.d", "e.f"])
  119. self.assertEqual(
  120. r, os.path.join("third_party", "rust", "foo", "v6", "c.d", "e.f"))
  121. def test_os_crate_cargo_dir(self):
  122. # Test the inner dir is there.
  123. r = common.os_crate_cargo_dir("foo-bar", "1.2.3")
  124. self.assertEqual(
  125. r, os.path.join("third_party", "rust", "foo_bar", "v1", "crate"))
  126. # Test rel_path.
  127. r = common.os_crate_cargo_dir("foo-bar", "1.2.3", rel_path=["g", "h"])
  128. self.assertEqual(
  129. r,
  130. os.path.join("third_party", "rust", "foo_bar", "v1", "crate", "g",
  131. "h"))
  132. def test_crate_download_url(self):
  133. r = common.crate_download_url("foo", "1.2.3")
  134. self.assertEqual(r,
  135. "https://static.crates.io/crates/foo/foo-1.2.3.crate")
  136. def test_crate_view_url(self):
  137. r = common.crate_view_url("foo")
  138. self.assertEqual(r, "https://crates.io/crates/foo")