Skip to content

Commit

Permalink
Do not include version test classes in prod
Browse files Browse the repository at this point in the history
  • Loading branch information
angelikatyborska committed Sep 19, 2024
1 parent 24de682 commit 7a0fdc5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
4 changes: 4 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ config :bitstyles_phoenix,
trim_e2e_classes: [enabled: true]

config :phoenix, :json_library, Jason

if config_env() === :test do
config :bitstyles_phoenix, add_version_test_classes: true
end
24 changes: 18 additions & 6 deletions lib/bitstyles_phoenix/bitstyles.ex
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ defmodule BitstylesPhoenix.Bitstyles do
"u-fg-text" -> "u-fg-gray-30"
"u-fg-text-darker" -> "u-fg-gray-50"
"u-bg-gray-darker" -> "u-bg-gray-80"
"u-version-5-0-0" -> "u-version-4"
class -> class
end

class = test_only_downgrade(class, "u-version-5-0-0", "u-version-4")

downgrade_classname(class, target_version, {4, 3, 0})
end

Expand Down Expand Up @@ -127,10 +128,11 @@ defmodule BitstylesPhoenix.Bitstyles do
"u-border-radius-" <> variant -> "u-round--#{variant}"
"u-overflow-x-auto" -> "u-overflow--x"
"u-overflow-y-auto" -> "u-overflow--y"
"u-version-4" -> "u-version-2"
_ -> class
end

class = test_only_downgrade(class, "u-version-4", "u-version-2")

downgrade_classname(class, target_version, {3, 0, 0})
end

Expand All @@ -147,10 +149,11 @@ defmodule BitstylesPhoenix.Bitstyles do
"u-flex-grow-" <> number -> "u-flex__grow-#{number}"
"u-flex-wrap" -> "u-flex--wrap"
"u-flex-col" -> "u-flex--col"
"u-version-2" -> "u-version-1-4"
_ -> class
end

class = test_only_downgrade(class, "u-version-2", "u-version-1-4")

downgrade_classname(class, target_version, {1, 5, 0})
end

Expand All @@ -161,16 +164,17 @@ defmodule BitstylesPhoenix.Bitstyles do

defp downgrade_classname(class, target_version, {1, 4, 0}) do
# downgrading 1.4.0 -> 1.3.0
mapping =
class =
case class do
"u-grid-cols-" <> number -> "u-grid--#{number}-col"
"u-col-span-" <> number -> "u-grid__col-span-#{number}"
"u-col-start-" <> number -> "u-grid__col-#{number}"
"u-version-1-4" -> "u-version-1-3"
_ -> class
end

downgrade_classname(mapping, target_version, {1, 3, 0})
class = test_only_downgrade(class, "u-version-1-4", "u-version-1-3")

downgrade_classname(class, target_version, {1, 3, 0})
end

defp downgrade_classname(class, target_version, {1, 3, 0}) do
Expand All @@ -184,4 +188,12 @@ defmodule BitstylesPhoenix.Bitstyles do
Please upgrade bitsyles and set the `bitsyles_version` to the updated version.
""")
end

defp test_only_downgrade(class, from, to) do
if Application.get_env(:bitstyles_phoenix, :add_version_test_classes, false) do
if(class == from, do: to, else: class)
else
class
end
end
end
5 changes: 3 additions & 2 deletions test/bitstyles_phoenix/bitstyles_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ defmodule BitstylesPhoenix.BitstylesTest do
)
end

# TODO: delete
test "chained version" do
test "chained version downgrade" do
# those class changes are only added in the test environment, to test that chaining works
# and won't be available to the users of bitstyles_phoenix
assert classname("u-version-5-0-0", {5, 0, 1}) == "u-version-5-0-0"
assert classname("u-version-5-0-0", {5, 0, 0}) == "u-version-5-0-0"
assert classname("u-version-5-0-0", {4, 3, 0}) == "u-version-4"
Expand Down

0 comments on commit 7a0fdc5

Please sign in to comment.