Skip to content

Instantly share code, notes, and snippets.

@holman
Last active June 18, 2020 01:27
Show Gist options
  • Save holman/4bd27ba3950ee2ee79c3 to your computer and use it in GitHub Desktop.
Save holman/4bd27ba3950ee2ee79c3 to your computer and use it in GitHub Desktop.
A snapshot of the tests we use internally at GitHub to help edit our blog posts before they go out to everybody. For more information, take a peek at http://zachholman.com/posts/how-github-writes-blog-posts
require_relative "test_helper"
require "open-uri"
require "net/http"
class EmojiTest < Blog::Test
def test_no_emoji
posts.each do |post|
content = File.read(post)
refute_match /:[a-zA-Z0-9_]+:/, content,
"You have emoji in your post, which we try to avoid"
end
end
end
require_relative "test_helper"
require "open-uri"
require "net/http"
class ImagesTest < Blog::Test
def setup
@images = posts.map do |post|
content = File.read(post)
md = Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, tables: true)
html = md.render(content)
doc = Nokogiri::HTML(html)
doc.css("img").map do |img|
url = img.attribute("src").value
uri = URI(url)
response = Net::HTTP.get_response(uri)
{ :url => url, :response => response, :element => img }
end
end.flatten
end
def test_images_arent_too_big
large_images = @images.select { |img| img[:response].body.size.to_i > 1_000_000 }
error_message = large_images.map do |img|
" #{img[:response].body.size} bytes: #{img[:url]}"
end.join("\n")
assert large_images.empty?,
"The following images are over a megabyte in size... consider shrinking them:\n\n#{error_message}"
end
def test_images_have_alt_tags
@images.select do |img|
alt = img[:element].attribute("alt").value
no_alt_tags = (alt.strip == "" || (alt =~ /^screen shot /))
refute no_alt_tags,
"Images need an alt tag for accessibility. Your image should have a short, one or two word description of the image in the ![] parts of your post."
end
end
def test_images_are_dotcom_hosted
@images.select do |img|
url = img[:url]
assert_match /^https:\/\/cloud\.githubusercontent\.com/, url,
"Images should be hosted on GitHub. Try dragging an image into an issue comment, uploading it, and using that URL."
end
end
def test_image_proportions
@images.select do |img|
width, height = FastImage.size(img[:url])
assert width >= 1354,
"Screenshots should be retina-quality — meaning at least 1354 pixels across — so they look good on higher-resolution screens. If you're taking a shot of a small interface element, consider zooming the page with cmd+plus and taking a screenshot that way; the blog will scale it down for you."
assert (width/height.to_f) > 1.2,
"Screenshots should be roughly panoramic (more than a 1.2 width:height ratio). Try taking a wider, shorter screenshot so it reduces the amount of scoll necessary to view the content of the blog post."
end
end
end
require "fileutils"
require "rubygems"
require "bundler/setup"
require "minitest/autorun"
require "fastimage"
require "nokogiri"
require "redcarpet"
cat = <<-'cat'
.--.
`. \
\ \
. \
: .
| .
| :
| |
..._ ___ | |
`."".`''''""--..___ | |
,-\ \ ""-...__ _____________/ |
/ ` " ' `"""""""" .
\ L
(> \
/ \
\_ ___..---. L
`--' '. \
. \_
_/`. `.._
.' -. `.
/ __.-Y /''''''-...___,...--------.._ |
/ _." | / ' . \ '---..._ |
/ / / / _,. ' ,/ | |
\_,' _.' / /'' _,-' _| |
' / `-----'' / |
`...-' !_____)
cat
puts cat
puts "
Hello you lovely GitHub blog author person human!
This is test output from your blog post you just wrote. Unlike most tests in
life (and code!), it's *totally cool* to fail these tests. They're just here
to catch anything you might have missed while writing your draft.
Consider them suggestions rather than things you *need* to do before
publishing your post.
Happy shipping!
"
module Blog
class Test < MiniTest::Test
# All the posts we're interested in checking. This means we're looking at
# files that have changed on this particular branch we're on.
#
# Returns an Array of String filenames.
def posts
diffable_files = `git diff --name-only --diff-filter=ACMRTUXB origin/master... | grep .md`.split("\n")
posts = diffable_files.select do |filename|
File.ctime(filename) > Time.new(2014,9,3)
end
posts
end
end
end
require_relative "test_helper"
class TodayTest < Blog::Test
def test_doesnt_start_with_today
posts.each do |post|
content = File.read(post)
body_text = content.split("\n").delete_if do |line|
line[0] == "#" || line.strip == ""
end
refute body_text.first =~ /Today/, "Posts usually shouldn't start with \"Today\""
end
end
end
@WhyNotHugo
Copy link

Have you guys considered open-sourcing your entire blog platform? I'm guessing it's entirely homemade, and might be of interest to others (even if only to study it).

@elliotblackburn
Copy link

@hobarrera I'm pretty sure they use Jekyll which allows writing of posts in markdown and is written in Ruby, which would make adding tests quite easy if you setup your own.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment