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

chore: add more unit tests #15

Merged
merged 1 commit into from
Feb 17, 2023
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/5022a74d146f487581821fd1c3435437)](https://www.codacy.com/gh/LinuxSuRen/md-exec/dashboard?utm_source=github.com&utm_medium=referral&utm_content=LinuxSuRen/md-exec&utm_campaign=Badge_Grade)
[![Codacy Badge](https://app.codacy.com/project/badge/Coverage/5022a74d146f487581821fd1c3435437)](https://www.codacy.com/gh/LinuxSuRen/md-exec/dashboard?utm_source=github.com&utm_medium=referral&utm_content=LinuxSuRen/md-exec&utm_campaign=Badge_Coverage)
![GitHub All Releases](https://img.shields.io/github/downloads/linuxsuren/md-exec/total)

## Usage
`md-exec` could exec the commands in the Markdown files.
Expand Down
21 changes: 9 additions & 12 deletions cli/golang_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,18 @@ func (s *GolangScript) Run() (err error) {
s.Content = strings.ReplaceAll(s.Content, "#!title: "+s.Title, "")

var shellFile string
if shellFile, err = writeAsShell(fmt.Sprintf(sampleGo, s.Content), s.Dir); err != nil {
fmt.Println(err)
return
}
if shellFile, err = writeAsShell(fmt.Sprintf(sampleGo, s.Content), s.Dir); err == nil {
goSourceFile := fmt.Sprintf("%s.go", shellFile)
os.Rename(shellFile, goSourceFile)

goSourceFile := fmt.Sprintf("%s.go", shellFile)
os.Rename(shellFile, goSourceFile)
if !s.KeepScripts {
defer func() {
_ = os.RemoveAll(goSourceFile)
}()
}

if !s.KeepScripts {
defer func() {
_ = os.RemoveAll(goSourceFile)
}()
err = s.Execer.RunCommandInDir("go", s.Dir, "run", path.Base(goSourceFile))
}

err = s.Execer.RunCommandInDir("go", s.Dir, "run", path.Base(goSourceFile))
return
}

Expand Down
19 changes: 8 additions & 11 deletions cli/python_runner.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cli

import (
"fmt"
"os"
"path"
)
Expand All @@ -14,17 +13,15 @@ type PythonScript struct {
// Run executes the script
func (s *PythonScript) Run() (err error) {
var shellFile string
if shellFile, err = writeAsShell(s.Content, s.Dir); err != nil {
fmt.Println(err)
return
}
if !s.KeepScripts {
defer func() {
_ = os.RemoveAll(shellFile)
}()
}
if shellFile, err = writeAsShell(s.Content, s.Dir); err == nil {
if !s.KeepScripts {
defer func() {
_ = os.RemoveAll(shellFile)
}()
}

err = s.Execer.RunCommandInDir("python3", s.Dir, path.Base(shellFile))
err = s.Execer.RunCommandInDir("python3", s.Dir, path.Base(shellFile))
}
return
}

Expand Down
4 changes: 3 additions & 1 deletion cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ func (o *option) runE(cmd *cobra.Command, args []string) (err error) {

if scriptRunners.Size() > 1 {
for {
err = o.executeScripts(scriptRunners)
if err = o.executeScripts(scriptRunners); err != nil {
fmt.Fprintln(cmd.ErrOrStderr(), err.Error())
}

if !o.loop {
break
Expand Down
12 changes: 12 additions & 0 deletions cli/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,15 @@ func TestNewRootCommand(t *testing.T) {
})
}
}

func TestParseMarkdownRUnner(t *testing.T) {
opt := &option{}
runners, err := opt.parseMarkdownRunner("../README.md")
if assert.Nil(t, err) {
assert.True(t, len(runners) > 0)
assert.NotNil(t, runners.GetRunner("Variable Input Hello World"))
assert.NotNil(t, runners.GetRunner("Python Hello World"))
assert.NotNil(t, runners.GetRunner("Run long time"))
assert.NotNil(t, runners.GetRunner("Golang Hello World"))
}
}
40 changes: 19 additions & 21 deletions cli/shell_runner.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package cli

import (
"fmt"
"github.com/linuxsuren/http-downloader/pkg/installer"
"io"
"os"
"path"
"regexp"
"strings"

"github.com/linuxsuren/http-downloader/pkg/installer"

"github.com/AlecAivazis/survey/v2"
)

Expand Down Expand Up @@ -60,27 +60,25 @@ func (s *ShellScript) Run() (err error) {

func (s *ShellScript) runCmdLine(cmdLine, contextDir string, keepScripts bool) (err error) {
var shellFile string
if shellFile, err = writeAsShell(cmdLine, contextDir); err != nil {
fmt.Println(err)
return
}
if !keepScripts {
defer func() {
_ = os.RemoveAll(shellFile)
}()
}
if shellFile, err = writeAsShell(cmdLine, contextDir); err == nil {
if !keepScripts {
defer func() {
_ = os.RemoveAll(shellFile)
}()
}

if s.ShellType == "shell" || s.ShellType == "" {
s.ShellType = "bash"
}
if s.ShellType == "shell" || s.ShellType == "" {
s.ShellType = "bash"
}

is := installer.Installer{
Provider: "github",
}
if err = is.CheckDepAndInstall(map[string]string{
s.ShellType: s.ShellType,
}); err == nil {
err = s.Execer.RunCommandInDir(s.ShellType, contextDir, path.Base(shellFile))
is := installer.Installer{
Provider: "github",
}
if err = is.CheckDepAndInstall(map[string]string{
s.ShellType: s.ShellType,
}); err == nil {
err = s.Execer.RunCommandInDir(s.ShellType, contextDir, path.Base(shellFile))
}
}
return
}
Expand Down
22 changes: 22 additions & 0 deletions cli/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cli

import (
"testing"

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

func TestScriptRunners(t *testing.T) {
runners := ScriptRunners{}
assert.Equal(t, 0, runners.Size())
assert.Nil(t, runners.GetRunner("fake"))
assert.Equal(t, []string{}, runners.GetTitles())

runners = NewScriptRunners()
quitRunner := runners.GetRunner("Quit")
if assert.NotNil(t, quitRunner) {
assert.Equal(t, "Quit", quitRunner.GetTitle())
assert.Nil(t, quitRunner.Run())
}
assert.Equal(t, []string{"Quit"}, runners.GetTitles())
}