Skip to content

Instantly share code, notes, and snippets.

View moxley's full-sized avatar

Moxley Stratton moxley

View GitHub Profile
@moxley
moxley / test_reporter.ex
Created March 3, 2023 00:12
Help track down errors and other unwanted output in ExUnit tests when `mix test --trace` isn't sufficient
defmodule TestReporter do
# Why: This is useful for tracking down intermittent errors and output in tests.
# What: Reports the test that ran in the test suite, in the order that they ran, and includes each test's ordinal number
# in the order of the test's finish time.
#
# Usage:
# 1. Save this file to `tests/support` or anywhere it'll get picked up by the Elixir compiler.
# 2. In test_helper.ex, add this GenServer as an ExUnit formater, like this:
# `ExUnit.start(exclude: [:external], formatters: [ExUnit.CLIFormatter, TestReporter])`
# 3. Run the test suite. At the end of the run, it will list all the tests that ran, in the order that they ran.
@moxley
moxley / gclean
Created July 13, 2020 18:30
Script that cleans up local repo after merging a PR
#!/bin/sh
# Example:
# gclean master
trunk="$1"
if [ -z "${trunk}" ]; then
echo "Usage: $0 TRUNK"
exit 2
fi
@moxley
moxley / typescript_interface_generator.ex
Created July 7, 2020 16:32
Generate a Typescript interface from an OpenApiSpex Schema struct
defmodule MoxleyApiUtil.TypescriptInterfaceGenerator do
alias OpenApiSpex.Schema
def to_ts_interface(%Schema{type: :object} = schema) do
header = "interface #{schema.title} {"
body = Enum.map(schema.properties, &to_ts_property_line/1) |> Enum.join("\n")
header <> "\n" <> body <> "\n}\n"
end
defp to_ts_property_line({name, schema}) do
@moxley
moxley / initial_schema.exs
Last active November 27, 2019 16:33
Ecto flattened migrations, initial migration
defmodule MyApp.Repo.Migrations.InitialSchema do
use Ecto.Migration
def up do
db = Application.fetch_env!(:my_app, MyApp.Repo)
file_path = "priv/repo/initial_schema.sql"
{output, errno} =
System.cmd(
"psql",
@moxley
moxley / phoenix_live_view_error_handle_params.md
Created August 12, 2019 00:29
Phoenix LiveView error using handle_params/3

Error message:

[error] GenServer #PID<0.1901.0> terminating
** (MatchError) no match of right hand side value: :external
    (phoenix_live_view) lib/phoenix_live_view/channel.ex:152: Phoenix.LiveView.Channel.call_mount_handle_params/2
    (phoenix_live_view) lib/phoenix_live_view/channel.ex:443: Phoenix.LiveView.Channel.verified_mount/9
    (stdlib) gen_server.erl:637: :gen_server.try_dispatch/4
    (stdlib) gen_server.erl:711: :gen_server.handle_msg/6
    (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message: {:mount, Phoenix.LiveView.Channel}
@moxley
moxley / dialyzer_test.ex
Created July 7, 2019 03:24
Unexplainable Dialyzer warning
defmodule DialyzerTest do
def call(map) when is_map(map) do
with :ok <- validate_1(map[:value_1]),
:ok <- validate_2(map[:value_2]) do
:ok
else
{:error, reason} ->
# lib/dialyzer_test.ex:10:exact_eq
# The test :bad_value_2 == :bad_value_1 can never evaluate to 'true'.
if !map[:required] && reason == :bad_value_1 do
@moxley
moxley / phoenix_controller_test_error.md
Last active June 24, 2019 02:10
Error message when running controller test for Elixir Phoenix
(UndefinedFunctionError) function MyApp.FooController.init/1 is undefined or private

The problem is that your test module is named the same as your controller. Instead, it should have Test appended to the end of the module name.

@moxley
moxley / rerun_dialyzer.md
Created June 18, 2019 00:58
Rerun dialyzer after changing dependency in an Elixir project

Oh noes! Are you getting an error reported by dialyzer that is related to a mix.exs dependency, and you want to modify the dependency to try to change the behavior?

With a regular dependency, let's say you modify a file inside of deps/foo/*. Run the following:

  1. mix deps.compile foo
  2. mix dialyzer.build && mix dialyzer

With a dependency specified with the file: ... option, you only need:

@moxley
moxley / install_elixir_ubuntu.sh
Last active May 4, 2019 22:20
Install Elixir 1.8.1/Erlang 21.3 onto Ubuntu 16.04
#!/bin/sh -e
# Run this script as root
apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y locales
sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen && \
dpkg-reconfigure --frontend=noninteractive locales && \
update-locale LANG=en_US.UTF-8
ENV LANG en_US.UTF-8
@moxley
moxley / Dockerfile
Created January 31, 2019 18:31
Dockerfile for Elixir releases
#
# Build image
#
FROM elixir:1.7.4-alpine AS build
# Set the locale
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8