Skip to content

Commit

Permalink
add copy and paste tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bradwood committed Nov 24, 2020
1 parent 1a08d55 commit 58533fd
Show file tree
Hide file tree
Showing 11 changed files with 493 additions and 68 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,18 @@ jobs:
- name: Get dependencies
run: |
go get -v -t -d ./...
go get -u golang.org/x/lint/golint
if [ -f Gopkg.toml ]; then
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
dep ensure
fi
- name: Lint
run: golint ./cmd .

- name: Build
run: go build -v .

- name: Test
run: go test -v -cover ./...

26 changes: 13 additions & 13 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ name: Release
on:
create:
tags:
- v*
- v*

jobs:
release:
name: Release on GitHub
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v1
- name: Check out code
uses: actions/checkout@v1

- name: Validate Go releaser config
uses: docker://goreleaser/goreleaser:latest
with:
args: check
- name: Validate Go releaser config
uses: docker://goreleaser/goreleaser:latest
with:
args: check

- name: Create release on GitHub
uses: docker://goreleaser/goreleaser:latest
with:
args: release
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
- name: Create release on GitHub
uses: docker://goreleaser/goreleaser:latest
with:
args: release
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
105 changes: 55 additions & 50 deletions cmd/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import (
"github.com/xanzy/go-gitlab"
)

// RuneReader interface to allow STDIN dependency injection
type RuneReader interface {
ReadRune() (rune, int, error)
}

var copyCmd = &cobra.Command{
Use: "copy",
Short: "Copy GitLab Snippet from STDIN",
Expand All @@ -25,73 +30,73 @@ func init() {
// Copy implements the copy command
func Copy(cmd *cobra.Command, args []string) {
git := GetGitlabClient()
copy(args, &git)
}

// TODO: write test for this
func copy(args []string, git *gitlab.Client) {

stat, _ := os.Stdin.Stat()

if (stat.Mode() & os.ModeCharDevice) == 0 { // we were piped into

// read stdin
reader := bufio.NewReader(os.Stdin)
var output []rune

for {
input, _, err := reader.ReadRune()
if err != nil && err == io.EOF {
break
}
output = append(output, input)
}
copy(args, git, viper.GetString("clipboard_name"), reader)
} else { // invoked without a pipe or redirect
fmt.Println("ERROR: Please pipe something into STDIN")
os.Exit(1)
}
}

// search snippets for a clipboard with the correct name to update
snippets, _, err := git.Snippets.ListSnippets(&gitlab.ListSnippetsOptions{})
func copy(args []string, git gitlab.Client, clipboardName string, reader RuneReader) {

BailOnError(err)
// read stdin
// reader := bufio.NewReader(os.Stdin)
var output []rune

for {
input, _, err := reader.ReadRune()
if err != nil && err == io.EOF {
break
}
output = append(output, input)
}

var clipboardFound bool = false
var clipboardID int
// search snippets for a clipboard with the correct name to update
snippets, _, err := git.Snippets.ListSnippets(&gitlab.ListSnippetsOptions{})

for _, item := range snippets {
BailOnError(err)

if item.Title == viper.GetString("clipboard_name") {
clipboardFound = true
clipboardID = item.ID
break
}
}
var clipboardFound bool = false
var clipboardID int

// create a new snippet
if !clipboardFound {
snippetoptions := &gitlab.CreateSnippetOptions{
Title: gitlab.String(viper.GetString("clipboard_name")),
FileName: gitlab.String(viper.GetString("clipboard_name")),
Content: gitlab.String(string(output)),
Visibility: gitlab.Visibility(gitlab.PrivateVisibility),
}
for _, item := range snippets {

_, _, err = git.Snippets.CreateSnippet(snippetoptions)
if item.Title == clipboardName {
clipboardFound = true
clipboardID = item.ID
break
}
}

BailOnError(err)
// create a new snippet
if !clipboardFound {
snippetoptions := &gitlab.CreateSnippetOptions{
Title: gitlab.String(clipboardName),
FileName: gitlab.String(clipboardName),
Content: gitlab.String(string(output)),
Visibility: gitlab.Visibility(gitlab.PrivateVisibility),
}

} else { // update existing snippet
snippetoptions := &gitlab.UpdateSnippetOptions{
Title: gitlab.String(viper.GetString("clipboard_name")),
FileName: gitlab.String(viper.GetString("clipboard_name")),
Content: gitlab.String(string(output)),
Visibility: gitlab.Visibility(gitlab.PrivateVisibility),
}
_, _, err = git.Snippets.CreateSnippet(snippetoptions)

_, _, err = git.Snippets.UpdateSnippet(clipboardID, snippetoptions)
BailOnError(err)

BailOnError(err)
} else { // update existing snippet
snippetoptions := &gitlab.UpdateSnippetOptions{
Title: gitlab.String(clipboardName),
FileName: gitlab.String(clipboardName),
Content: gitlab.String(string(output)),
Visibility: gitlab.Visibility(gitlab.PrivateVisibility),
}

} else {
fmt.Println("ERROR: Please pipe something into STDIN")
os.Exit(1)
_, _, err = git.Snippets.UpdateSnippet(clipboardID, snippetoptions)

BailOnError(err)
}

}
107 changes: 107 additions & 0 deletions cmd/copy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package cmd

import (
"bytes"
"fmt"
"net/http"
"testing"
// "github.com/stretchr/testify/assert"
)

func TestCopyUpdate(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)

mux.HandleFunc("/api/v4/snippets", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
mustWriteHTTPResponse(t, w, "../testdata/list_snippets_paste.json")
})

mux.HandleFunc("/api/v4/snippets/41", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT")
testURL(t, r, "/api/v4/snippets/41") // this line verifies the update PUT occurs
fmt.Fprint(w,
`{
"id": 1,
"title": "test",
"description": "description of snippet",
"visibility": "internal",
"author": {
"id": 1,
"username": "john_smith",
"email": "[email protected]",
"name": "John Smith",
"state": "active",
"created_at": "2012-05-23T08:00:58Z"
},
"expires_at": null,
"updated_at": "2012-06-28T10:52:04Z",
"created_at": "2012-06-28T10:52:04Z",
"project_id": null,
"web_url": "http://example.com/snippets/1",
"raw_url": "http://example.com/snippets/1/raw",
"ssh_url_to_repo": "ssh://[email protected]:snippets/1.git",
"http_url_to_repo": "https://gitlab.example.com/snippets/1.git",
"file_name": "renamed.md",
"files": [
{
"path": "renamed.md",
"raw_url": "https://gitlab.example.com/-/snippets/1/raw/master/renamed.md"
}
]
}`)
})

var emptyStringArray []string

copy(emptyStringArray, *client, "glsnip", bytes.NewBufferString("std in"))

}

func TestCopyCreate(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)

mux.HandleFunc("/api/v4/snippets", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
testMethod(t, r, "GET")
mustWriteHTTPResponse(t, w, "../testdata/list_snippets_paste_no_glsnip.json")
} else if r.Method == "POST" {
fmt.Fprint(w,
`{
"id": 1,
"title": "test",
"description": "description of snippet",
"visibility": "internal",
"author": {
"id": 1,
"username": "john_smith",
"email": "[email protected]",
"name": "John Smith",
"state": "active",
"created_at": "2012-05-23T08:00:58Z"
},
"expires_at": null,
"updated_at": "2012-06-28T10:52:04Z",
"created_at": "2012-06-28T10:52:04Z",
"project_id": null,
"web_url": "http://example.com/snippets/1",
"raw_url": "http://example.com/snippets/1/raw",
"ssh_url_to_repo": "ssh://[email protected]:snippets/1.git",
"http_url_to_repo": "https://gitlab.example.com/snippets/1.git",
"file_name": "renamed.md",
"files": [
{
"path": "renamed.md",
"raw_url": "https://gitlab.example.com/-/snippets/1/raw/master/renamed.md"
}
]
}`)
}
})

var emptyStringArray []string

copy(emptyStringArray, *client, "glsnip", bytes.NewBufferString("std in"))

}
13 changes: 8 additions & 5 deletions cmd/paste.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,27 @@ func init() {
// Paste implements the paste command
func Paste(cmd *cobra.Command, args []string) {
git := GetGitlabClient()
paste(args, git)
output := paste(args, git, viper.GetString("clipboard_name"))
fmt.Print(output)
}

// TODO: write test for this
func paste(args []string, git gitlab.Client) {
func paste(args []string, git gitlab.Client, clipboardName string) string {

var output string

snippets, _, err := git.Snippets.ListSnippets(&gitlab.ListSnippetsOptions{})

BailOnError(err)

for _, item := range snippets {

if item.Title == viper.GetString("clipboard_name") {
if item.Title == clipboardName {
snip, _, err := git.Snippets.SnippetContent(item.ID)
BailOnError(err)
fmt.Print(string(snip))
output = string(snip)
break
}
}

return output
}
30 changes: 30 additions & 0 deletions cmd/paste_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cmd

import (
"fmt"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
)

func TestPaste(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)

mux.HandleFunc("/api/v4/snippets", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
mustWriteHTTPResponse(t, w, "../testdata/list_snippets_paste.json")
})

mux.HandleFunc("/api/v4/snippets/41/raw", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, "PASTED_DATA")
})

var emptyStringArray []string

output := paste(emptyStringArray, *client, "glsnip")

assert.Equal(t, "PASTED_DATA", output)
}
Loading

0 comments on commit 58533fd

Please sign in to comment.