Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
stages:
- build
- test

before_script:
Expand All @@ -14,7 +15,18 @@ before_script:
.job_template: &job_definition
stage: test
script:
- make test
- ls
- make testci

Build Linux binary:
stage: build
image: golang
script:
- go get -u github.com/gosimple/slug
- make build
artifacts:
paths:
- dist/

#
# Alpine
Expand Down
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
sudo: false
language: ruby
language: go
os:
- linux
- osx
Expand All @@ -10,5 +10,5 @@ before_script:
- export PATH=$PATH:bats/bin

script:
- go get -u github.com/gosimple/slug
- make test

2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- Ported the script to go

## [1.0.2] - 2018-09-29
### Fixed
Expand Down
12 changes: 8 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
.PHONY: help build alias clean deploy test

test: .test.mk ## Run the test suite using bats
.test.mk: test/suite.bats slugify
testci: .testci.mk ## Run the test suite in the CI (don't build)
.test.mk: test/suite.bats build
bats $<
touch $@
.testci.mk: test/suite.bats
bats $<
touch $@

Expand All @@ -12,9 +16,9 @@ deploy: build ## Deploy to now.sh
alias: deploy ## Create an alias on now.sh
cd dist && now alias slugify

build: dist/slugify test ## "Build" the script (ie: move it to dist/)
dist/slugify: dist slugify
cp slugify dist/./
build: dist/slugify ## "Build" the script (ie: move it to dist/)
dist/slugify: dist slugify.go
go build -o $@

dist: ## Create the dist folder
mkdir -p dist
Expand Down
58 changes: 0 additions & 58 deletions slugify

This file was deleted.

66 changes: 66 additions & 0 deletions slugify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package main

import (
"bufio"
"fmt"
"os"

"github.com/gosimple/slug"
)

func main() {

var pipe_lines []string
var args_lines []string

if weAreInAPipe() {
pipe_lines = readPipe()
} else if len(os.Args[1:]) > 0 {
args_lines = os.Args[1:]
} else {
var version = "2.0.0"
fmt.Println("version ", version)
fmt.Println("Usage: ", os.Args[0], " text")
fmt.Println(" echo text | ", os.Args[0])
os.Exit(1)
}

if len(pipe_lines) > len(args_lines) {
slugify(pipe_lines)
} else {
slugify(args_lines)
}
}

func slugify(lines []string) {
for _, line := range lines {
fmt.Println(slug.Make(line))
}
}

func weAreInAPipe() bool {
// Cargo culted from https://flaviocopes.com/go-shell-pipes/
// Not sure how some part of that works, but don't care
info, err := os.Stdin.Stat()
if err != nil {
panic(err)
}

if (info.Mode() & os.ModeCharDevice) == os.ModeCharDevice {
return false
}

return true
}

func readPipe() []string {
// Cargo culted from https://stackoverflow.com/questions/5884154/read-text-file-into-string-array-and-write
reader := bufio.NewReader(os.Stdin)
var lines []string
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}

return lines
}
34 changes: 21 additions & 13 deletions test/suite.bats
Original file line number Diff line number Diff line change
@@ -1,33 +1,41 @@
#!/usr/bin/env bats

slugify="./slugify"
load 'test_helper/bats-support/load'
load 'test_helper/bats-assert/load'

slugify="dist/slugify"

@test "slugify binary exist in the expected place" {
run test -e $slugify
assert_success
}

@test "slug \"lol #@ slug mY l1fe béébé\" via pipe" {
result=$(echo "lol #@ slug mY l1fe béébé" | $slugify)
test "${result}" = "lol-slug-my-l1fe-b-b"
run bash -c "echo 'lol #@ slug mY l1fe béébé' | $slugify"
assert_output "lol-at-slug-my-l1fe-beebe"
}

@test "slug \"lol #@ slug mY l1fe béébé\" as argument" {
result=$($slugify "lol #@ slug mY l1fe béébé")
test "${result}" = "lol-slug-my-l1fe-b-b"
run $slugify "lol #@ slug mY l1fe béébé"
assert_output "lol-at-slug-my-l1fe-beebe"
}

@test "slug \"Slug me bébé!!1!\" via pipe" {
result=$(echo 'Slug me bébé!!1!' | $slugify)
test "${result}" = "slug-me-b-b-1"
run bash -c "echo 'Slug me bébé!!1!' | $slugify"
assert_output "slug-me-bebe-1"
}

@test "slug \"Slug me bébé!!1!\" as argument" {
result=$($slugify 'Slug me bébé!!1!')
test "${result}" = "slug-me-b-b-1"
run $slugify 'Slug me bébé!!1!'
assert_output "slug-me-bebe-1"
}

@test "slug \"I'm no slug.\" via pipe" {
result=$(echo "I'm no slug." | $slugify)
test "${result}" = "i-m-no-slug"
run bash -c "echo \"I'm no slug.\" | $slugify"
assert_output "im-no-slug"
}

@test "slug \"I'm no slug.\" as argument" {
result=$($slugify "I'm no slug.")
test "${result}" = "i-m-no-slug"
run $slugify "I'm no slug."
assert_output "im-no-slug"
}
8 changes: 8 additions & 0 deletions test/test_helper/bats-assert/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
language: bash
before_install:
- ./script/install-bats.sh
- git clone --depth 1 https://github.com/ztombol/bats-support ../bats-support
before_script:
- export PATH="${HOME}/.local/bin:${PATH}"
script:
- bats test
39 changes: 39 additions & 0 deletions test/test_helper/bats-assert/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Change Log

All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).


## [0.3.0] - 2016-03-22

### Removed

- Move `fail()` to `bats-support`


## [0.2.0] - 2016-03-11

### Added

- `refute()` to complement `assert()`
- `npm` support

### Fixed

- Not consuming the `--` when stopping option parsing in
`assert_output`, `refute_output`, `assert_line` and `refute_line`


## 0.1.0 - 2016-02-16

### Added

- Reporting arbitrary failures with `fail()`
- Generic assertions with `assert()` and `assert_equal()`
- Testing exit status with `assert_success()` and `assert_failure()`
- Testing output with `assert_output()` and `refute_output()`
- Testing individual lines with `assert_line()` and `refute_line()`


[0.3.0]: https://github.com/ztombol/bats-assert/compare/v0.2.0...v0.3.0
[0.2.0]: https://github.com/ztombol/bats-assert/compare/v0.1.0...v0.2.0
Loading