Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[testing] add TestDoImport #156

Merged
merged 2 commits into from
May 5, 2019
Merged
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
42 changes: 0 additions & 42 deletions commands.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"bufio"
"errors"
"fmt"
"os"
Expand All @@ -13,7 +12,6 @@ import (
"github.com/motemen/ghq/cmdutil"
"github.com/motemen/ghq/logger"
"github.com/urfave/cli"
"golang.org/x/sync/errgroup"
"golang.org/x/xerrors"
)

Expand Down Expand Up @@ -331,46 +329,6 @@ func doLook(c *cli.Context) error {
}
}

func doImport(c *cli.Context) error {
var parallel = c.Bool("parallel")
g := &getter{
update: c.Bool("update"),
shallow: c.Bool("shallow"),
ssh: c.Bool("p"),
vcs: c.String("vcs"),
silent: c.Bool("silent"),
}
if parallel {
// force silent in parallel import
g.silent = true
}

eg := &errgroup.Group{}
sem := make(chan struct{}, 6)
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
if parallel {
eg.Go(func() error {
sem <- struct{}{}
defer func() { <-sem }()
if err := g.get(line); err != nil {
logger.Log("error", err.Error())
}
return nil
})
} else {
if err := g.get(line); err != nil {
logger.Log("error", err.Error())
}
}
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("While reading input: %s", err)
}
return eg.Wait()
}

func doRoot(c *cli.Context) error {
var (
w = c.App.Writer
Expand Down
17 changes: 17 additions & 0 deletions helpers_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"io/ioutil"
"net/url"
"os"
Expand Down Expand Up @@ -89,3 +90,19 @@ func capture(block func()) (string, string, error) {

return string(bufOut), string(bufErr), nil
}

func captureWithInput(in []string, block func()) (string, string, error) {
rIn, wIn, err := os.Pipe()
if err != nil {
return "", "", err
}
defer rIn.Close()
var stdin *os.File
os.Stdin, stdin = rIn, os.Stdin
defer func() { os.Stdin = stdin }()
for _, line := range in {
fmt.Fprintln(wIn, line)
}
wIn.Close()
return capture(block)
}
51 changes: 51 additions & 0 deletions import.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main

import (
"bufio"
"fmt"
"os"

"github.com/motemen/ghq/logger"
"github.com/urfave/cli"
"golang.org/x/sync/errgroup"
)

func doImport(c *cli.Context) error {
var parallel = c.Bool("parallel")
g := &getter{
update: c.Bool("update"),
shallow: c.Bool("shallow"),
ssh: c.Bool("p"),
vcs: c.String("vcs"),
silent: c.Bool("silent"),
}
if parallel {
// force silent in parallel import
g.silent = true
}

eg := &errgroup.Group{}
sem := make(chan struct{}, 6)
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
if parallel {
eg.Go(func() error {
sem <- struct{}{}
defer func() { <-sem }()
if err := g.get(line); err != nil {
logger.Log("error", err.Error())
}
return nil
})
} else {
if err := g.get(line); err != nil {
logger.Log("error", err.Error())
}
}
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("While reading input: %s", err)
}
return eg.Wait()
}
8 changes: 7 additions & 1 deletion logger/log.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package logger

import (
"io"
"os"

"github.com/motemen/go-colorine"
Expand Down Expand Up @@ -28,7 +29,12 @@ var logger = colorine.NewLogger(
}, colorine.Info)

func init() {
logger.SetOutput(os.Stderr)
SetOutput(os.Stderr)
}

// SetOutput sets log output writer
func SetOutput(w io.Writer) {
logger.SetOutput(w)
}

// Log output
Expand Down