Skip to content

Instantly share code, notes, and snippets.

View devonestes's full-sized avatar

Devon Estes devonestes

View GitHub Profile
@devonestes
devonestes / credo_ci.exs
Created November 11, 2021 10:35
Run credo only on changed files
defmodule Mix.Tasks.Credo.Ci do
@shortdoc "Run Credo only on files that have changed since the last merge commit."
@moduledoc """
Runs Credo in CI with somewhat special behavior.
If the branch being tested is `master`, it runs Credo on all files. Otherwise, it only runs
Credo on the files that have changed since the last merge commit, and it will fail CI if any
check doesn't pass for those specific files.
"""
# Given that we have a `User` module, that looks something like this:
defmodule User do
defstruct [:name]
def changeset(user, params) do
# ...
end
end
@devonestes
devonestes / README.md
Last active March 7, 2020 12:51
README for Muzak

Muzak

What is Muzak?

Muzak is a mutation testing library for Elixir and Erlang applications.

What is Mutation Testing?

Mutation testing is a way of systematically introducing bugs into your code and then running your tests to see if any of them fail. If you introduce a bug in your code

@devonestes
devonestes / with_example.ex
Created February 8, 2020 16:55
Further refactoring of a with statement
# Step 1
def create_subscription(email, plan_id, payment_method_id) do
with %User{customer_id: nil, name: name} = user <-
Repo.get_by(User, email: email),
{:ok, %Stripe.Customer{id: customer_id}} <-
Stripe.Customer.create(%{
name: name,
email: email,
payment_method: payment_method_id,
# My GenServer
defmodule My.GenServer do
def start_link() do
GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
end
def init(_) do
{:ok, []}
end
@devonestes
devonestes / test_failures.ex
Created March 3, 2019 14:22
Calculate if there are any intermittent test failures
def intermittent_failures(
%{id: suite_id},
spec \\ %{10 => 100, 9 => 80, 8 => 60, 7 => 45, 6 => 32, 5 => 25, 4 => 16, 3 => 9}
) do
limit = spec |> Map.values() |> Enum.max()
last_100_runs =
from(r in Run,
where: r.suite_id == ^suite_id,
order_by: [desc: r.inserted_at],
@devonestes
devonestes / comprehension.exs
Created February 27, 2019 14:32
Filter map varieties in Elixir
for num <- [1, 2, 3], num < 3, do: num * 2
@devonestes
devonestes / trace_issue.escript
Created August 1, 2018 10:17
Reproduction escript for trace/3 wierdness
#!/usr/bin/env escript
%% -*- erlang -*-
%%! -smp enable -sname wtf
main(_) ->
process_flag(trap_exit, true),
ToMap = lists:seq(1, 50),
MapFun = fun() ->
lists:map(fun(N) -> N end, ToMap)
end,
start_runner(MapFun),
defmodule SendAfter do
def send_after(pid, message, nanoseconds) do
{_, os_name} = :os.type()
increment =
if os_name == :Windows do
# monotonic time for Windows in miliseconds, in Linux and MacOS it's nanoseconds
# so we'll need to convert to miliseconds for windows
nanoseconds / 1_000_000
else
nanoseconds
import Header from './header.jsx';
function headerFun(input) {
<h1>{input.name}</h1>
}
function DesktopHeader(input) {
<Header {...{input, headerFun}} />
}