Compare commits
4 Commits
f148fe4fcd
...
dcf3032d0e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dcf3032d0e | ||
|
|
6a5269f30a | ||
|
|
5d040b4062 | ||
|
|
5186edc2e9 |
226
app/.credo.exs
Normal file
226
app/.credo.exs
Normal file
@ -0,0 +1,226 @@
|
|||||||
|
# This file contains the configuration for Credo and you are probably reading
|
||||||
|
# this after creating it with `mix credo.gen.config`.
|
||||||
|
#
|
||||||
|
# If you find anything wrong or unclear in this file, please report an
|
||||||
|
# issue on GitHub: https://github.com/rrrene/credo/issues
|
||||||
|
#
|
||||||
|
%{
|
||||||
|
#
|
||||||
|
# You can have as many configs as you like in the `configs:` field.
|
||||||
|
configs: [
|
||||||
|
%{
|
||||||
|
#
|
||||||
|
# Run any config using `mix credo -C <name>`. If no config name is given
|
||||||
|
# "default" is used.
|
||||||
|
#
|
||||||
|
name: "default",
|
||||||
|
#
|
||||||
|
# These are the files included in the analysis:
|
||||||
|
files: %{
|
||||||
|
#
|
||||||
|
# You can give explicit globs or simply directories.
|
||||||
|
# In the latter case `**/*.{ex,exs}` will be used.
|
||||||
|
#
|
||||||
|
included: [
|
||||||
|
"lib/",
|
||||||
|
"src/",
|
||||||
|
"test/",
|
||||||
|
"web/",
|
||||||
|
"apps/*/lib/",
|
||||||
|
"apps/*/src/",
|
||||||
|
"apps/*/test/",
|
||||||
|
"apps/*/web/"
|
||||||
|
],
|
||||||
|
excluded: [~r"/_build/", ~r"/deps/", ~r"/node_modules/"]
|
||||||
|
},
|
||||||
|
#
|
||||||
|
# Load and configure plugins here:
|
||||||
|
#
|
||||||
|
plugins: [],
|
||||||
|
#
|
||||||
|
# If you create your own checks, you must specify the source files for
|
||||||
|
# them here, so they can be loaded by Credo before running the analysis.
|
||||||
|
#
|
||||||
|
requires: ["lib/firehose/checks/"],
|
||||||
|
#
|
||||||
|
# If you want to enforce a style guide and need a more traditional linting
|
||||||
|
# experience, you can change `strict` to `true` below:
|
||||||
|
#
|
||||||
|
strict: false,
|
||||||
|
#
|
||||||
|
# To modify the timeout for parsing files, change this value:
|
||||||
|
#
|
||||||
|
parse_timeout: 5000,
|
||||||
|
#
|
||||||
|
# If you want to use uncolored output by default, you can change `color`
|
||||||
|
# to `false` below:
|
||||||
|
#
|
||||||
|
color: true,
|
||||||
|
#
|
||||||
|
# You can customize the parameters of any check by adding a second element
|
||||||
|
# to the tuple.
|
||||||
|
#
|
||||||
|
# To disable a check put `false` as second element:
|
||||||
|
#
|
||||||
|
# {Credo.Check.Design.DuplicatedCode, false}
|
||||||
|
#
|
||||||
|
checks: %{
|
||||||
|
enabled: [
|
||||||
|
#
|
||||||
|
## Consistency Checks
|
||||||
|
#
|
||||||
|
{Credo.Check.Consistency.ExceptionNames, []},
|
||||||
|
{Credo.Check.Consistency.LineEndings, []},
|
||||||
|
{Credo.Check.Consistency.ParameterPatternMatching, []},
|
||||||
|
{Credo.Check.Consistency.SpaceAroundOperators, []},
|
||||||
|
{Credo.Check.Consistency.SpaceInParentheses, []},
|
||||||
|
{Credo.Check.Consistency.TabsOrSpaces, []},
|
||||||
|
|
||||||
|
#
|
||||||
|
## Design Checks
|
||||||
|
#
|
||||||
|
# You can customize the priority of any check
|
||||||
|
# Priority values are: `low, normal, high, higher`
|
||||||
|
#
|
||||||
|
{Credo.Check.Design.AliasUsage,
|
||||||
|
[priority: :low, if_nested_deeper_than: 2, if_called_more_often_than: 0]},
|
||||||
|
{Credo.Check.Design.TagFIXME, []},
|
||||||
|
# You can also customize the exit_status of each check.
|
||||||
|
# If you don't want TODO comments to cause `mix credo` to fail, just
|
||||||
|
# set this value to 0 (zero).
|
||||||
|
#
|
||||||
|
{Credo.Check.Design.TagTODO, [exit_status: 2]},
|
||||||
|
|
||||||
|
#
|
||||||
|
## Readability Checks
|
||||||
|
#
|
||||||
|
{Credo.Check.Readability.AliasOrder, []},
|
||||||
|
{Credo.Check.Readability.FunctionNames, []},
|
||||||
|
{Credo.Check.Readability.LargeNumbers, []},
|
||||||
|
{Credo.Check.Readability.MaxLineLength, [priority: :low, max_length: 120]},
|
||||||
|
{Credo.Check.Readability.ModuleAttributeNames, []},
|
||||||
|
{Credo.Check.Readability.ModuleDoc, []},
|
||||||
|
{Credo.Check.Readability.ModuleNames, []},
|
||||||
|
{Credo.Check.Readability.ParenthesesInCondition, []},
|
||||||
|
{Credo.Check.Readability.ParenthesesOnZeroArityDefs, []},
|
||||||
|
{Credo.Check.Readability.PipeIntoAnonymousFunctions, []},
|
||||||
|
{Credo.Check.Readability.PredicateFunctionNames, []},
|
||||||
|
{Credo.Check.Readability.PreferImplicitTry, []},
|
||||||
|
{Credo.Check.Readability.RedundantBlankLines, []},
|
||||||
|
{Credo.Check.Readability.Semicolons, []},
|
||||||
|
{Credo.Check.Readability.SpaceAfterCommas, []},
|
||||||
|
{Credo.Check.Readability.StringSigils, []},
|
||||||
|
{Credo.Check.Readability.TrailingBlankLine, []},
|
||||||
|
{Credo.Check.Readability.TrailingWhiteSpace, []},
|
||||||
|
{Credo.Check.Readability.UnnecessaryAliasExpansion, []},
|
||||||
|
{Credo.Check.Readability.VariableNames, []},
|
||||||
|
{Credo.Check.Readability.WithSingleClause, []},
|
||||||
|
|
||||||
|
#
|
||||||
|
## Refactoring Opportunities
|
||||||
|
#
|
||||||
|
{Credo.Check.Refactor.Apply, []},
|
||||||
|
{Credo.Check.Refactor.CondStatements, []},
|
||||||
|
{Credo.Check.Refactor.CyclomaticComplexity, []},
|
||||||
|
{Credo.Check.Refactor.FilterCount, []},
|
||||||
|
{Credo.Check.Refactor.FilterFilter, []},
|
||||||
|
{Credo.Check.Refactor.FunctionArity, []},
|
||||||
|
{Credo.Check.Refactor.LongQuoteBlocks, []},
|
||||||
|
{Credo.Check.Refactor.MapJoin, []},
|
||||||
|
{Credo.Check.Refactor.MatchInCondition, []},
|
||||||
|
{Credo.Check.Refactor.NegatedConditionsInUnless, []},
|
||||||
|
{Credo.Check.Refactor.NegatedConditionsWithElse, []},
|
||||||
|
{Credo.Check.Refactor.Nesting, []},
|
||||||
|
{Credo.Check.Refactor.RedundantWithClauseResult, []},
|
||||||
|
{Credo.Check.Refactor.RejectReject, []},
|
||||||
|
{Credo.Check.Refactor.UnlessWithElse, []},
|
||||||
|
{Credo.Check.Refactor.WithClauses, []},
|
||||||
|
|
||||||
|
#
|
||||||
|
## Warnings
|
||||||
|
#
|
||||||
|
{Credo.Check.Warning.ApplicationConfigInModuleAttribute, []},
|
||||||
|
{Credo.Check.Warning.BoolOperationOnSameValues, []},
|
||||||
|
{Credo.Check.Warning.Dbg, []},
|
||||||
|
{Credo.Check.Warning.ExpensiveEmptyEnumCheck, []},
|
||||||
|
{Credo.Check.Warning.IExPry, []},
|
||||||
|
{Credo.Check.Warning.IoInspect, []},
|
||||||
|
{Credo.Check.Warning.MissedMetadataKeyInLoggerConfig, []},
|
||||||
|
{Credo.Check.Warning.OperationOnSameValues, []},
|
||||||
|
{Credo.Check.Warning.OperationWithConstantResult, []},
|
||||||
|
{Credo.Check.Warning.RaiseInsideRescue, []},
|
||||||
|
{Credo.Check.Warning.SpecWithStruct, []},
|
||||||
|
{Credo.Check.Warning.StructFieldAmount, []},
|
||||||
|
{Credo.Check.Warning.UnsafeExec, []},
|
||||||
|
{Credo.Check.Warning.UnusedEnumOperation, []},
|
||||||
|
{Credo.Check.Warning.UnusedFileOperation, []},
|
||||||
|
{Credo.Check.Warning.UnusedKeywordOperation, []},
|
||||||
|
{Credo.Check.Warning.UnusedListOperation, []},
|
||||||
|
{Credo.Check.Warning.UnusedMapOperation, []},
|
||||||
|
{Credo.Check.Warning.UnusedPathOperation, []},
|
||||||
|
{Credo.Check.Warning.UnusedRegexOperation, []},
|
||||||
|
{Credo.Check.Warning.UnusedStringOperation, []},
|
||||||
|
{Credo.Check.Warning.UnusedTupleOperation, []},
|
||||||
|
{Credo.Check.Warning.WrongTestFilename, []},
|
||||||
|
|
||||||
|
#
|
||||||
|
## Custom Checks
|
||||||
|
#
|
||||||
|
{Firehose.Checks.NoConnShadowing, []}
|
||||||
|
],
|
||||||
|
disabled: [
|
||||||
|
#
|
||||||
|
# Checks scheduled for next check update (opt-in for now)
|
||||||
|
{Credo.Check.Refactor.UtcNowTruncate, []},
|
||||||
|
|
||||||
|
#
|
||||||
|
# Controversial and experimental checks (opt-in, just move the check to `:enabled`
|
||||||
|
# and be sure to use `mix credo --strict` to see low priority checks)
|
||||||
|
#
|
||||||
|
{Credo.Check.Consistency.MultiAliasImportRequireUse, []},
|
||||||
|
{Credo.Check.Consistency.UnusedVariableNames, []},
|
||||||
|
{Credo.Check.Design.DuplicatedCode, []},
|
||||||
|
{Credo.Check.Design.SkipTestWithoutComment, []},
|
||||||
|
{Credo.Check.Readability.AliasAs, []},
|
||||||
|
{Credo.Check.Readability.BlockPipe, []},
|
||||||
|
{Credo.Check.Readability.ImplTrue, []},
|
||||||
|
{Credo.Check.Readability.MultiAlias, []},
|
||||||
|
{Credo.Check.Readability.NestedFunctionCalls, []},
|
||||||
|
{Credo.Check.Readability.OneArityFunctionInPipe, []},
|
||||||
|
{Credo.Check.Readability.OnePipePerLine, []},
|
||||||
|
{Credo.Check.Readability.SeparateAliasRequire, []},
|
||||||
|
{Credo.Check.Readability.SingleFunctionToBlockPipe, []},
|
||||||
|
{Credo.Check.Readability.SinglePipe, []},
|
||||||
|
{Credo.Check.Readability.Specs, []},
|
||||||
|
{Credo.Check.Readability.StrictModuleLayout, []},
|
||||||
|
{Credo.Check.Readability.WithCustomTaggedTuple, []},
|
||||||
|
{Credo.Check.Refactor.ABCSize, []},
|
||||||
|
{Credo.Check.Refactor.AppendSingleItem, []},
|
||||||
|
{Credo.Check.Refactor.CondInsteadOfIfElse, []},
|
||||||
|
{Credo.Check.Refactor.DoubleBooleanNegation, []},
|
||||||
|
{Credo.Check.Refactor.FilterReject, []},
|
||||||
|
{Credo.Check.Refactor.IoPuts, []},
|
||||||
|
{Credo.Check.Refactor.MapMap, []},
|
||||||
|
{Credo.Check.Refactor.ModuleDependencies, []},
|
||||||
|
{Credo.Check.Refactor.NegatedIsNil, []},
|
||||||
|
{Credo.Check.Refactor.PassAsyncInTestCases, []},
|
||||||
|
{Credo.Check.Refactor.PipeChainStart, []},
|
||||||
|
{Credo.Check.Refactor.RejectFilter, []},
|
||||||
|
{Credo.Check.Refactor.VariableRebinding, []},
|
||||||
|
{Credo.Check.Warning.LazyLogging, []},
|
||||||
|
{Credo.Check.Warning.LeakyEnvironment, []},
|
||||||
|
{Credo.Check.Warning.MapGetUnsafePass, []},
|
||||||
|
{Credo.Check.Warning.MixEnv, []},
|
||||||
|
{Credo.Check.Warning.UnsafeToAtom, []}
|
||||||
|
# {Credo.Check.Warning.UnusedOperation, [{MyMagicModule, [:fun1, :fun2]}]}
|
||||||
|
|
||||||
|
# {Credo.Check.Refactor.MapInto, []},
|
||||||
|
|
||||||
|
#
|
||||||
|
# Custom checks can be created using `mix credo.gen.check`.
|
||||||
|
#
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@ -61,7 +61,8 @@ config :logger, :default_formatter,
|
|||||||
config :phoenix, :json_library, Jason
|
config :phoenix, :json_library, Jason
|
||||||
|
|
||||||
config :blogex,
|
config :blogex,
|
||||||
blogs: [Firehose.EngineeringBlog, Firehose.ReleaseNotes]
|
blogs: [Firehose.EngineeringBlog, Firehose.ReleaseNotes],
|
||||||
|
show_drafts: true
|
||||||
|
|
||||||
# Import environment specific config. This must remain at the bottom
|
# Import environment specific config. This must remain at the bottom
|
||||||
# of this file so it overrides the configuration defined above.
|
# of this file so it overrides the configuration defined above.
|
||||||
|
|||||||
@ -13,6 +13,9 @@ config :swoosh, api_client: Swoosh.ApiClient.Req
|
|||||||
# Disable Swoosh Local Memory Storage
|
# Disable Swoosh Local Memory Storage
|
||||||
config :swoosh, local: false
|
config :swoosh, local: false
|
||||||
|
|
||||||
|
# Hide draft blog posts in production
|
||||||
|
config :blogex, show_drafts: false
|
||||||
|
|
||||||
# Do not print debug messages in production
|
# Do not print debug messages in production
|
||||||
config :logger, level: :info
|
config :logger, level: :info
|
||||||
|
|
||||||
|
|||||||
44
app/lib/firehose/checks/no_conn_shadowing.ex
Normal file
44
app/lib/firehose/checks/no_conn_shadowing.ex
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
defmodule Firehose.Checks.NoConnShadowing do
|
||||||
|
use Credo.Check,
|
||||||
|
base_priority: :normal,
|
||||||
|
category: :readability,
|
||||||
|
explanations: [
|
||||||
|
check: """
|
||||||
|
Conn shadowing (`conn = get(conn, ...)`) makes Phoenix controller tests
|
||||||
|
noisy. Use pipe chains instead:
|
||||||
|
|
||||||
|
body = conn |> get("/path") |> html_response(200)
|
||||||
|
|
||||||
|
Run `./refactor_conn_aliasing.sh <file>` to fix automatically.
|
||||||
|
"""
|
||||||
|
]
|
||||||
|
|
||||||
|
@http_verbs ~w(get post put patch delete head options)a
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def run(%SourceFile{} = source_file, params) do
|
||||||
|
issue_meta = IssueMeta.for(source_file, params)
|
||||||
|
|
||||||
|
source_file
|
||||||
|
|> Credo.Code.prewalk(&traverse(&1, &2, issue_meta))
|
||||||
|
|> Enum.reverse()
|
||||||
|
end
|
||||||
|
|
||||||
|
defp traverse({:=, meta, [{:conn, _, _}, {verb, _, [{:conn, _, _} | _]}]} = ast, issues, issue_meta)
|
||||||
|
when verb in @http_verbs do
|
||||||
|
issue = issue_for(issue_meta, meta[:line], verb)
|
||||||
|
{ast, [issue | issues]}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp traverse(ast, issues, _issue_meta) do
|
||||||
|
{ast, issues}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp issue_for(issue_meta, line_no, verb) do
|
||||||
|
format_issue(
|
||||||
|
issue_meta,
|
||||||
|
message: "Conn shadowing detected (`conn = #{verb}(conn, ...)`). Run `./refactor_conn_aliasing.sh <file>` to fix.",
|
||||||
|
line_no: line_no
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -5,8 +5,7 @@ defmodule FirehoseWeb.BlogTest do
|
|||||||
|
|
||||||
defp visit_engineering_page(conn, suffix \\ "") do
|
defp visit_engineering_page(conn, suffix \\ "") do
|
||||||
path = "/blog/engineering" <> suffix
|
path = "/blog/engineering" <> suffix
|
||||||
conn = get(conn, path)
|
body = conn |> get(path) |> html_response(200)
|
||||||
body = html_response(conn, 200)
|
|
||||||
assert body =~ "Engineering Blog"
|
assert body =~ "Engineering Blog"
|
||||||
assert body =~ "firehose"
|
assert body =~ "firehose"
|
||||||
body
|
body
|
||||||
@ -14,8 +13,7 @@ defmodule FirehoseWeb.BlogTest do
|
|||||||
|
|
||||||
defp visit_engineering_path(conn, suffix) do
|
defp visit_engineering_path(conn, suffix) do
|
||||||
path = "/blog/engineering" <> suffix
|
path = "/blog/engineering" <> suffix
|
||||||
conn = get(conn, path)
|
body = conn |> get(path) |> html_response(200)
|
||||||
body = html_response(conn, 200)
|
|
||||||
assert body =~ "firehose"
|
assert body =~ "firehose"
|
||||||
body
|
body
|
||||||
end
|
end
|
||||||
@ -38,23 +36,19 @@ defmodule FirehoseWeb.BlogTest do
|
|||||||
|
|
||||||
describe "input validation" do
|
describe "input validation" do
|
||||||
test "GET /blog/nonexistent returns 404", %{conn: conn} do
|
test "GET /blog/nonexistent returns 404", %{conn: conn} do
|
||||||
conn = get(conn, "/blog/nonexistent")
|
assert conn |> get("/blog/nonexistent") |> html_response(404)
|
||||||
assert html_response(conn, 404)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test "GET /blog/engineering?page=abc falls back to page 1", %{conn: conn} do
|
test "GET /blog/engineering?page=abc falls back to page 1", %{conn: conn} do
|
||||||
body = visit_engineering_page(conn, "")
|
assert conn |> get("/blog/engineering?page=abc") |> html_response(200) =~ "Engineering Blog"
|
||||||
assert body =~ "Engineering Blog"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test "GET /blog/engineering?page=-1 falls back to page 1", %{conn: conn} do
|
test "GET /blog/engineering?page=-1 falls back to page 1", %{conn: conn} do
|
||||||
body = visit_engineering_page(conn, "")
|
assert conn |> get("/blog/engineering?page=-1") |> html_response(200) =~ "Engineering Blog"
|
||||||
assert body =~ "Engineering Blog"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test "GET /blog/engineering?page=0 falls back to page 1", %{conn: conn} do
|
test "GET /blog/engineering?page=0 falls back to page 1", %{conn: conn} do
|
||||||
body = visit_engineering_page(conn, "?page=0")
|
assert conn |> get("/blog/engineering?page=0") |> html_response(200) =~ "Engineering Blog"
|
||||||
assert body =~ "Engineering Blog"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test "GET /blog/engineering/nonexistent-post returns 404", %{conn: conn} do
|
test "GET /blog/engineering/nonexistent-post returns 404", %{conn: conn} do
|
||||||
@ -66,85 +60,99 @@ defmodule FirehoseWeb.BlogTest do
|
|||||||
|
|
||||||
describe "release notes blog (HTML)" do
|
describe "release notes blog (HTML)" do
|
||||||
test "GET /blog/releases returns HTML index", %{conn: conn} do
|
test "GET /blog/releases returns HTML index", %{conn: conn} do
|
||||||
conn = get(conn, "/blog/releases")
|
body = conn |> get("/blog/releases") |> html_response(200)
|
||||||
body = html_response(conn, 200)
|
|
||||||
assert body =~ "Release Notes"
|
assert body =~ "Release Notes"
|
||||||
assert body =~ "v0.1.0 Released"
|
assert body =~ "v0.1.0 Released"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "GET /blog/releases/:slug returns HTML post", %{conn: conn} do
|
test "GET /blog/releases/:slug returns HTML post", %{conn: conn} do
|
||||||
conn = get(conn, "/blog/releases/v0-1-0")
|
body = conn |> get("/blog/releases/v0-1-0") |> html_response(200)
|
||||||
body = html_response(conn, 200)
|
|
||||||
assert body =~ "v0.1.0 Released"
|
assert body =~ "v0.1.0 Released"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "GET /blog/releases/tag/:tag returns HTML tag page", %{conn: conn} do
|
test "GET /blog/releases/tag/:tag returns HTML tag page", %{conn: conn} do
|
||||||
conn = get(conn, "/blog/releases/tag/elixir")
|
body = conn |> get("/blog/releases/tag/elixir") |> html_response(200)
|
||||||
body = html_response(conn, 200)
|
|
||||||
assert body =~ ~s(tagged "elixir")
|
assert body =~ ~s(tagged "elixir")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "engineering blog (JSON API)" do
|
describe "engineering blog (JSON API)" do
|
||||||
test "GET /api/blog/engineering returns post index", %{conn: conn} do
|
test "GET /api/blog/engineering returns post index", %{conn: conn} do
|
||||||
conn = conn |> put_req_header("accept", "application/json")
|
assert %{"blog" => "engineering", "posts" => posts} =
|
||||||
conn = get(conn, "/api/blog/engineering")
|
conn
|
||||||
assert %{"blog" => "engineering", "posts" => posts} = json_response(conn, 200)
|
|> put_req_header("accept", "application/json")
|
||||||
|
|> get("/api/blog/engineering")
|
||||||
|
|> json_response(200)
|
||||||
|
|
||||||
assert is_list(posts)
|
assert is_list(posts)
|
||||||
refute Enum.empty?(posts)
|
refute Enum.empty?(posts)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "GET /api/blog/engineering/:slug returns a post", %{conn: conn} do
|
test "GET /api/blog/engineering/:slug returns a post", %{conn: conn} do
|
||||||
conn = conn |> put_req_header("accept", "application/json")
|
assert %{"id" => "hello-world", "title" => "Hello World"} =
|
||||||
conn = get(conn, "/api/blog/engineering/hello-world")
|
conn
|
||||||
assert %{"id" => "hello-world", "title" => "Hello World"} = json_response(conn, 200)
|
|> put_req_header("accept", "application/json")
|
||||||
|
|> get("/api/blog/engineering/hello-world")
|
||||||
|
|> json_response(200)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "GET /api/blog/engineering/:slug returns 404 for missing post", %{conn: conn} do
|
test "GET /api/blog/engineering/:slug returns 404 for missing post", %{conn: conn} do
|
||||||
conn = conn |> put_req_header("accept", "application/json")
|
assert conn
|
||||||
conn = get(conn, "/api/blog/engineering/nonexistent")
|
|> put_req_header("accept", "application/json")
|
||||||
assert response(conn, 404)
|
|> get("/api/blog/engineering/nonexistent")
|
||||||
|
|> response(404)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "GET /api/blog/engineering/feed.xml returns RSS", %{conn: conn} do
|
test "GET /api/blog/engineering/feed.xml returns RSS", %{conn: conn} do
|
||||||
conn = get(conn, "/api/blog/engineering/feed.xml")
|
response = conn |> get("/api/blog/engineering/feed.xml")
|
||||||
assert response(conn, 200) =~ "<rss"
|
assert response(response, 200) =~ "<rss"
|
||||||
assert response_content_type(conn, :xml)
|
assert response_content_type(response, :xml)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "GET /api/blog/engineering/tag/:tag returns JSON with posts", %{conn: conn} do
|
test "GET /api/blog/engineering/tag/:tag returns JSON with posts", %{conn: conn} do
|
||||||
conn = conn |> put_req_header("accept", "application/json")
|
assert %{"blog" => "engineering", "tag" => "elixir", "posts" => posts} =
|
||||||
conn = get(conn, "/api/blog/engineering/tag/elixir")
|
conn
|
||||||
assert %{"blog" => "engineering", "tag" => "elixir", "posts" => posts} = json_response(conn, 200)
|
|> put_req_header("accept", "application/json")
|
||||||
|
|> get("/api/blog/engineering/tag/elixir")
|
||||||
|
|> json_response(200)
|
||||||
|
|
||||||
assert is_list(posts)
|
assert is_list(posts)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "release notes blog (JSON API)" do
|
describe "release notes blog (JSON API)" do
|
||||||
test "GET /api/blog/releases returns post index", %{conn: conn} do
|
test "GET /api/blog/releases returns post index", %{conn: conn} do
|
||||||
conn = conn |> put_req_header("accept", "application/json")
|
assert %{"blog" => "release_notes", "posts" => posts} =
|
||||||
conn = get(conn, "/api/blog/releases")
|
conn
|
||||||
assert %{"blog" => "release_notes", "posts" => posts} = json_response(conn, 200)
|
|> put_req_header("accept", "application/json")
|
||||||
|
|> get("/api/blog/releases")
|
||||||
|
|> json_response(200)
|
||||||
|
|
||||||
assert is_list(posts)
|
assert is_list(posts)
|
||||||
refute Enum.empty?(posts)
|
refute Enum.empty?(posts)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "GET /api/blog/releases/:slug returns a post", %{conn: conn} do
|
test "GET /api/blog/releases/:slug returns a post", %{conn: conn} do
|
||||||
conn = conn |> put_req_header("accept", "application/json")
|
assert %{"id" => "v0-1-0", "title" => "v0.1.0 Released"} =
|
||||||
conn = get(conn, "/api/blog/releases/v0-1-0")
|
conn
|
||||||
assert %{"id" => "v0-1-0", "title" => "v0.1.0 Released"} = json_response(conn, 200)
|
|> put_req_header("accept", "application/json")
|
||||||
|
|> get("/api/blog/releases/v0-1-0")
|
||||||
|
|> json_response(200)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "GET /api/blog/releases/feed.xml returns RSS", %{conn: conn} do
|
test "GET /api/blog/releases/feed.xml returns RSS", %{conn: conn} do
|
||||||
conn = get(conn, "/api/blog/releases/feed.xml")
|
response = conn |> get("/api/blog/releases/feed.xml")
|
||||||
assert response(conn, 200) =~ "<rss"
|
assert response(response, 200) =~ "<rss"
|
||||||
assert response_content_type(conn, :xml)
|
assert response_content_type(response, :xml)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "GET /api/blog/releases/tag/:tag returns JSON with posts", %{conn: conn} do
|
test "GET /api/blog/releases/tag/:tag returns JSON with posts", %{conn: conn} do
|
||||||
conn = conn |> put_req_header("accept", "application/json")
|
assert %{"blog" => "release_notes", "tag" => "elixir", "posts" => posts} =
|
||||||
conn = get(conn, "/api/blog/releases/tag/elixir")
|
conn
|
||||||
assert %{"blog" => "release_notes", "tag" => "elixir", "posts" => posts} = json_response(conn, 200)
|
|> put_req_header("accept", "application/json")
|
||||||
|
|> get("/api/blog/releases/tag/elixir")
|
||||||
|
|> json_response(200)
|
||||||
|
|
||||||
assert is_list(posts)
|
assert is_list(posts)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -2,8 +2,7 @@ defmodule FirehoseWeb.PageControllerTest do
|
|||||||
use FirehoseWeb.ConnCase
|
use FirehoseWeb.ConnCase
|
||||||
|
|
||||||
test "GET /", %{conn: conn} do
|
test "GET /", %{conn: conn} do
|
||||||
conn = get(conn, ~p"/")
|
body = conn |> get(~p"/") |> html_response(200)
|
||||||
body = html_response(conn, 200)
|
|
||||||
assert body =~ "Drinking from the firehose"
|
assert body =~ "Drinking from the firehose"
|
||||||
assert body =~ "Willem van den Ende"
|
assert body =~ "Willem van den Ende"
|
||||||
end
|
end
|
||||||
|
|||||||
@ -110,6 +110,11 @@ defmodule Blogex do
|
|||||||
* `Blogex.Router` — mountable Plug router
|
* `Blogex.Router` — mountable Plug router
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@doc "Returns true if draft posts should be visible (dev/test environments)."
|
||||||
|
def show_drafts? do
|
||||||
|
Application.get_env(:blogex, :show_drafts, false)
|
||||||
|
end
|
||||||
|
|
||||||
defdelegate blogs, to: Blogex.Registry
|
defdelegate blogs, to: Blogex.Registry
|
||||||
defdelegate get_blog!(blog_id), to: Blogex.Registry
|
defdelegate get_blog!(blog_id), to: Blogex.Registry
|
||||||
defdelegate get_blog(blog_id), to: Blogex.Registry
|
defdelegate get_blog(blog_id), to: Blogex.Registry
|
||||||
|
|||||||
@ -73,8 +73,14 @@ defmodule Blogex.Blog do
|
|||||||
@doc "Returns the base URL path for this blog."
|
@doc "Returns the base URL path for this blog."
|
||||||
def base_path, do: @blog_base_path
|
def base_path, do: @blog_base_path
|
||||||
|
|
||||||
@doc "Returns all published posts, newest first."
|
@doc "Returns all visible posts, newest first. Drafts are included in dev/test."
|
||||||
def all_posts, do: Enum.filter(@posts, & &1.published)
|
def all_posts do
|
||||||
|
if Blogex.show_drafts?() do
|
||||||
|
@posts
|
||||||
|
else
|
||||||
|
Enum.filter(@posts, & &1.published)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
@doc "Returns the N most recent published posts."
|
@doc "Returns the N most recent published posts."
|
||||||
def recent_posts(n \\ 5), do: Enum.take(all_posts(), n)
|
def recent_posts(n \\ 5), do: Enum.take(all_posts(), n)
|
||||||
|
|||||||
188
refactor_conn_aliasing.sh
Executable file
188
refactor_conn_aliasing.sh
Executable file
@ -0,0 +1,188 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
usage() {
|
||||||
|
cat <<'EOF'
|
||||||
|
Usage: refactor_conn_aliasing.sh [OPTIONS] FILE...
|
||||||
|
--dry-run Show diff without modifying files
|
||||||
|
--help Show usage
|
||||||
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
DRY_RUN=false
|
||||||
|
FILES=()
|
||||||
|
|
||||||
|
while [[ $# -gt 0 ]]; do
|
||||||
|
case "$1" in
|
||||||
|
--dry-run) DRY_RUN=true; shift ;;
|
||||||
|
--help) usage; exit 0 ;;
|
||||||
|
-*) echo "Unknown option: $1" >&2; usage >&2; exit 1 ;;
|
||||||
|
*) FILES+=("$1"); shift ;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
if [[ ${#FILES[@]} -eq 0 ]]; then
|
||||||
|
echo "Error: no files specified" >&2
|
||||||
|
usage >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
for file in "${FILES[@]}"; do
|
||||||
|
if [[ ! -f "$file" ]]; then
|
||||||
|
echo "Warning: $file not found, skipping" >&2
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
|
||||||
|
tmpfile=$(mktemp)
|
||||||
|
trap "rm -f '$tmpfile'" EXIT
|
||||||
|
|
||||||
|
awk '
|
||||||
|
# Detect trigger line: conn = VERB(conn, ARGS)
|
||||||
|
# where VERB is get/post/put/patch/delete/head/options
|
||||||
|
/^[[:space:]]*conn = (get|post|put|patch|delete|head|options)\(conn, / {
|
||||||
|
trigger_line = $0
|
||||||
|
# Extract leading whitespace
|
||||||
|
match($0, /^[[:space:]]*/)
|
||||||
|
indent = substr($0, RSTART, RLENGTH)
|
||||||
|
|
||||||
|
# Extract verb and args from: conn = verb(conn, args)
|
||||||
|
rest = $0
|
||||||
|
sub(/^[[:space:]]*conn = /, "", rest)
|
||||||
|
# rest is now: verb(conn, args)
|
||||||
|
paren_pos = index(rest, "(")
|
||||||
|
verb = substr(rest, 1, paren_pos - 1)
|
||||||
|
# args portion: everything after "conn, " up to the trailing ")"
|
||||||
|
inner = substr(rest, paren_pos + 1)
|
||||||
|
sub(/\)$/, "", inner)
|
||||||
|
# inner is: conn, args
|
||||||
|
sub(/^conn, /, "", inner)
|
||||||
|
args = inner
|
||||||
|
|
||||||
|
# Read the next non-blank line
|
||||||
|
triggered = 1
|
||||||
|
next
|
||||||
|
}
|
||||||
|
|
||||||
|
triggered == 1 {
|
||||||
|
# Skip blank lines, accumulating them
|
||||||
|
if ($0 ~ /^[[:space:]]*$/) {
|
||||||
|
blank_lines = blank_lines $0 "\n"
|
||||||
|
next
|
||||||
|
}
|
||||||
|
|
||||||
|
next_line = $0
|
||||||
|
triggered = 0
|
||||||
|
|
||||||
|
# Now look ahead: count how many subsequent lines (until scope boundary)
|
||||||
|
# reference "conn" — to decide Case 4 vs Cases 1-3
|
||||||
|
# We already have next_line. Check if next_line references conn.
|
||||||
|
# Then peek further lines.
|
||||||
|
|
||||||
|
# For simplicity: check if next_line matches Case 1, 2, or 3 patterns.
|
||||||
|
# If it does, check the line AFTER that for more conn references (Case 4 override).
|
||||||
|
|
||||||
|
# Case 1: var = helper(conn, status)
|
||||||
|
# helpers: html_response, json_response, text_response, response, redirected_to
|
||||||
|
case1 = 0
|
||||||
|
if (match(next_line, /^[[:space:]]*([a-z_]+) = (html_response|json_response|text_response|response|redirected_to)\(conn, [^)]+\)$/, m1)) {
|
||||||
|
case1 = 1
|
||||||
|
c1_var = m1[1]
|
||||||
|
c1_helper = m1[2]
|
||||||
|
# Extract status from helper(conn, status)
|
||||||
|
match(next_line, /\(conn, ([^)]+)\)/, m1s)
|
||||||
|
c1_status = m1s[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
# Case 2: assert helper(conn, status) with optional =~ "..."
|
||||||
|
case2 = 0
|
||||||
|
if (match(next_line, /^[[:space:]]*assert (html_response|json_response|text_response|response|redirected_to)\(conn, ([^)]+)\)(.*)$/, m2)) {
|
||||||
|
case2 = 1
|
||||||
|
c2_helper = m2[1]
|
||||||
|
c2_status = m2[2]
|
||||||
|
c2_tail = m2[3]
|
||||||
|
}
|
||||||
|
|
||||||
|
# Case 3: assert %{...} = helper(conn, status)
|
||||||
|
case3 = 0
|
||||||
|
if (match(next_line, /^[[:space:]]*assert (%\{[^}]*\}) = (html_response|json_response|text_response|response|redirected_to)\(conn, ([^)]+)\)$/, m3)) {
|
||||||
|
case3 = 1
|
||||||
|
c3_pattern = m3[1]
|
||||||
|
c3_helper = m3[2]
|
||||||
|
c3_status = m3[3]
|
||||||
|
}
|
||||||
|
|
||||||
|
# If we matched Case 1, 2, or 3, emit the merged line
|
||||||
|
if (case1) {
|
||||||
|
print indent c1_var " = conn |> " verb "(" args ") |> " c1_helper "(" c1_status ")"
|
||||||
|
if (blank_lines != "") printf "%s", blank_lines
|
||||||
|
blank_lines = ""
|
||||||
|
next
|
||||||
|
}
|
||||||
|
if (case2) {
|
||||||
|
print indent "assert conn |> " verb "(" args ") |> " c2_helper "(" c2_status ")" c2_tail
|
||||||
|
if (blank_lines != "") printf "%s", blank_lines
|
||||||
|
blank_lines = ""
|
||||||
|
next
|
||||||
|
}
|
||||||
|
if (case3) {
|
||||||
|
print indent "assert " c3_pattern " = conn |> " verb "(" args ") |> " c3_helper "(" c3_status ")"
|
||||||
|
if (blank_lines != "") printf "%s", blank_lines
|
||||||
|
blank_lines = ""
|
||||||
|
next
|
||||||
|
}
|
||||||
|
|
||||||
|
# If next_line references conn at all, this is Case 4 territory
|
||||||
|
# (multiple uses without a recognized single-merge pattern)
|
||||||
|
if (next_line ~ /conn/) {
|
||||||
|
# Case 4: rename to response
|
||||||
|
print indent "response = conn |> " verb "(" args ")"
|
||||||
|
if (blank_lines != "") printf "%s", blank_lines
|
||||||
|
blank_lines = ""
|
||||||
|
# Replace conn with response in next_line
|
||||||
|
gsub(/conn/, "response", next_line)
|
||||||
|
print next_line
|
||||||
|
# Continue replacing conn->response in subsequent lines until scope boundary
|
||||||
|
renaming = 1
|
||||||
|
next
|
||||||
|
}
|
||||||
|
|
||||||
|
# No conn reference on next line — leave trigger unchanged (fallback)
|
||||||
|
print trigger_line
|
||||||
|
if (blank_lines != "") printf "%s", blank_lines
|
||||||
|
blank_lines = ""
|
||||||
|
print next_line
|
||||||
|
next
|
||||||
|
}
|
||||||
|
|
||||||
|
# Renaming mode for Case 4: replace conn with response until scope boundary
|
||||||
|
renaming == 1 {
|
||||||
|
# Scope boundary: blank line, "end", reduced indentation, or new conn = assignment
|
||||||
|
if ($0 ~ /^[[:space:]]*$/ || $0 ~ /^[[:space:]]*end$/ || $0 ~ /^[[:space:]]*conn =/) {
|
||||||
|
renaming = 0
|
||||||
|
print
|
||||||
|
next
|
||||||
|
}
|
||||||
|
gsub(/conn/, "response")
|
||||||
|
print
|
||||||
|
next
|
||||||
|
}
|
||||||
|
|
||||||
|
# Normal mode: pass through
|
||||||
|
{
|
||||||
|
if (blank_lines != "") {
|
||||||
|
printf "%s", blank_lines
|
||||||
|
blank_lines = ""
|
||||||
|
}
|
||||||
|
print
|
||||||
|
}
|
||||||
|
|
||||||
|
BEGIN { triggered = 0; renaming = 0; blank_lines = "" }
|
||||||
|
' "$file" > "$tmpfile"
|
||||||
|
|
||||||
|
if $DRY_RUN; then
|
||||||
|
diff -u "$file" "$tmpfile" || true
|
||||||
|
else
|
||||||
|
mv "$tmpfile" "$file"
|
||||||
|
echo "Refactored: $file"
|
||||||
|
fi
|
||||||
|
done
|
||||||
Loading…
x
Reference in New Issue
Block a user