Skip to content

Instantly share code, notes, and snippets.

Avatar

Devon Estes devonestes

View GitHub Profile
@devonestes
devonestes / with_example.ex
Created Feb 8, 2020
Further refactoring of a with statement
View with_example.ex
# 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,
@devonestes
devonestes / credo_ci.exs
Created Nov 11, 2021
Run credo only on changed files
View credo_ci.exs
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.
"""
View alternatives.ex
# 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 Mar 7, 2020
README for Muzak
View README.md

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

View xref_calls.exs
# 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 Mar 3, 2019
Calculate if there are any intermittent test failures
View test_failures.ex
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 Feb 27, 2019
Filter map varieties in Elixir
View comprehension.exs
for num <- [1, 2, 3], num < 3, do: num * 2
@devonestes
devonestes / trace_issue.escript
Created Aug 1, 2018
Reproduction escript for trace/3 wierdness
View trace_issue.escript
#!/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),
View send_after.ex
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
View desktop_header.jsx
import Header from './header.jsx';
function headerFun(input) {
<h1>{input.name}</h1>
}
function DesktopHeader(input) {
<Header {...{input, headerFun}} />
}