Skip to content
This repository has been archived by the owner on Feb 3, 2023. It is now read-only.

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
ingbyr committed Dec 21, 2021
1 parent 292990c commit 1790239
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 19 deletions.
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package cmd

import (
"github.com/ingbyr/gohost/display"
"github.com/ingbyr/gohost/host"
"github.com/spf13/cobra"
)

Expand All @@ -15,6 +16,7 @@ var rootCmd = &cobra.Command{
}

func Execute() {
host.M.SetCmdMode()
rootCmd.AddCommand(listCmd)
rootCmd.AddCommand(editCmd)
rootCmd.AddCommand(useCmd)
Expand Down
19 changes: 10 additions & 9 deletions host/host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@ import (
)

func TestNewHostByFileName(t *testing.T) {
M.SetMockMode()
var tests = []struct {
fileName string
want *Host
name string
want *Host
}{
{"", &Host{Name: "", FileName: "", Groups: []string{}}},
{"dev", &Host{Name: "dev", FileName: "dev", Groups: []string{}}},
{"dev_dev", &Host{Name: "dev", FileName: "dev_dev", Groups: []string{"dev"}}},
{"dev_test_prod_host1", &Host{Name: "host1", FileName: "dev_test_prod_host1", Groups: []string{"dev", "test", "prod"}}},
{"dev_test_prod_test_host1", &Host{Name: "host1", FileName: "dev_test_prod_test_host1", Groups: []string{"dev", "test", "prod", "test"}}},
{".txt", &Host{Name: "", FileName: ".txt", Groups: []string{}}},
{"dev.txt", &Host{Name: "dev", FileName: "dev.txt", Groups: []string{}}},
{"dev_dev.txt", &Host{Name: "dev", FileName: "dev_dev.txt", Groups: []string{"dev"}}},
{"dev_test_prod_host1.txt", &Host{Name: "host1", FileName: "dev_test_prod_host1.txt", Groups: []string{"dev", "test", "prod"}}},
{"dev_test_prod_test_host1.txt", &Host{Name: "host1", FileName: "dev_test_prod_test_host1.txt", Groups: []string{"dev", "test", "prod", "test"}}},
}
for _, test := range tests {
host := NewHostByFileName(test.fileName)
host := NewHostByFileName(test.name)
if diff := cmp.Diff(test.want, host, cmpopts.IgnoreFields(Host{}, "FilePath")); diff != "" {
t.Logf("input %v\n", test.fileName)
t.Logf("input %v\n", test.name)
t.Logf("diff \n%s\n", diff)
t.Fail()
}
Expand Down
11 changes: 7 additions & 4 deletions host/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@ func init() {
Groups: nil,
},
}
}

// setup default fs
M.SetFs(myfs.NewOsFs())
func (m *manager) SetCmdMode() {
m.SetFs(myfs.NewOsFs())
m.editor = editor.New(conf.Conf.Editor)
}

// setup editor
M.editor = editor.New(conf.Conf.Editor)
func (m *manager) SetMockMode() {
m.SetFs(myfs.NewMemFs())
}

func (m *manager) SetFs(newFs myfs.HostFs) {
Expand Down
9 changes: 4 additions & 5 deletions host/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package host

import (
"github.com/ingbyr/gohost/myfs"
"testing"
)

Expand All @@ -18,13 +17,13 @@ type hostTestOp struct {
initHosts []hosts
addGroups []string
deleteGroups []string
addHosts []hosts
deleteHosts []string
renameHosts [][2]string
addHosts []hosts
deleteHosts []string
renameHosts [][2]string
}

func TestManager_CreateRemoveNewHost(t *testing.T) {
M.SetFs(myfs.NewMemFs())
M.SetMockMode()
var tests = []hostTestOp{
{
initHosts: []hosts{
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ REG add "HKLM\SYSTEM\CurrentControlSet\services\dnscache" /v Start /t REG_DWORD
| Remove group for host | `gohost cg HOST_NAME -d(--delete) GROUP_NAME_1[,GROUP_NAME_2,...]` | `gohost cg file1 -d group3,group4` |
| List group | `gohost ls` | `gohost ls` |
| Rename group | `gohost mv -g(--group) GROUP_NAME NEW_GROUP_NAME` | `gohost mv -g group1 newGroup`|

| Delete group | `gohost rm -g GROUP_NAME` | `gohost rm -g group3` |
### Apply Group

| Description | Command | Example |
Expand Down

0 comments on commit 1790239

Please sign in to comment.