mirror of
				https://github.com/cheat/cheat.git
				synced 2025-11-04 07:45:28 +01:00 
			
		
		
		
	feat(tests): improve test coverage
This commit is contained in:
		
							
								
								
									
										22
									
								
								internal/config/color_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								internal/config/color_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,22 @@
 | 
			
		||||
package config
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"testing"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// TestColor asserts that colorization rules are properly respected
 | 
			
		||||
func TestColor(t *testing.T) {
 | 
			
		||||
 | 
			
		||||
	// mock a config
 | 
			
		||||
	conf := Config{}
 | 
			
		||||
 | 
			
		||||
	opts := map[string]interface{}{"--colorize": false}
 | 
			
		||||
	if conf.Color(opts) {
 | 
			
		||||
		t.Errorf("failed to respect --colorize (false)")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	opts = map[string]interface{}{"--colorize": true}
 | 
			
		||||
	if !conf.Color(opts) {
 | 
			
		||||
		t.Errorf("failed to respect --colorize (true)")
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										38
									
								
								internal/config/init_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								internal/config/init_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,38 @@
 | 
			
		||||
package config
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"os"
 | 
			
		||||
	"testing"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// TestInit asserts that configs are properly initialized
 | 
			
		||||
func TestInit(t *testing.T) {
 | 
			
		||||
 | 
			
		||||
	// initialize a temporary config file
 | 
			
		||||
	confFile, err := ioutil.TempFile("", "cheat-test")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Errorf("failed to create temp file: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// clean up the temp file
 | 
			
		||||
	defer os.Remove(confFile.Name())
 | 
			
		||||
 | 
			
		||||
	// initialize the config file
 | 
			
		||||
	conf := "mock config data"
 | 
			
		||||
	if err = Init(confFile.Name(), conf); err != nil {
 | 
			
		||||
		t.Errorf("failed to init config file: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// read back the config file contents
 | 
			
		||||
	bytes, err := ioutil.ReadFile(confFile.Name())
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Errorf("failed to read config file: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// assert that the contents were written correctly
 | 
			
		||||
	got := string(bytes)
 | 
			
		||||
	if got != conf {
 | 
			
		||||
		t.Errorf("failed to write configs: want: %s, got: %s", conf, got)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										53
									
								
								internal/config/path_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										53
									
								
								internal/config/path_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,53 @@
 | 
			
		||||
package config
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"os"
 | 
			
		||||
	"testing"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// TestPathConfigNotExists asserts that `Path` identifies non-existent config
 | 
			
		||||
// files
 | 
			
		||||
func TestPathConfigNotExists(t *testing.T) {
 | 
			
		||||
 | 
			
		||||
	// package (invalid) cheatpaths
 | 
			
		||||
	paths := []string{"/cheat-test-conf-does-not-exist"}
 | 
			
		||||
 | 
			
		||||
	// assert
 | 
			
		||||
	if _, err := Path(paths); err == nil {
 | 
			
		||||
		t.Errorf("failed to identify non-existent config file")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// TestPathConfigExists asserts that `Path` identifies existent config files
 | 
			
		||||
func TestPathConfigExists(t *testing.T) {
 | 
			
		||||
 | 
			
		||||
	// initialize a temporary config file
 | 
			
		||||
	confFile, err := ioutil.TempFile("", "cheat-test")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Errorf("failed to create temp file: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// clean up the temp file
 | 
			
		||||
	defer os.Remove(confFile.Name())
 | 
			
		||||
 | 
			
		||||
	// package cheatpaths
 | 
			
		||||
	paths := []string{
 | 
			
		||||
		"/cheat-test-conf-does-not-exist",
 | 
			
		||||
		confFile.Name(),
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// assert
 | 
			
		||||
	got, err := Path(paths)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Errorf("failed to identify config file: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	if got != confFile.Name() {
 | 
			
		||||
		t.Errorf(
 | 
			
		||||
			"failed to return config path: want: %s, got: %s",
 | 
			
		||||
			confFile.Name(),
 | 
			
		||||
			got,
 | 
			
		||||
		)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										34
									
								
								internal/sheet/colorize_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								internal/sheet/colorize_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,34 @@
 | 
			
		||||
package sheet
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
	"github.com/cheat/cheat/internal/config"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// TestColorize asserts that syntax-highlighting is correctly applied
 | 
			
		||||
func TestColorize(t *testing.T) {
 | 
			
		||||
 | 
			
		||||
	// mock configs
 | 
			
		||||
	conf := config.Config{
 | 
			
		||||
		Formatter: "terminal16m",
 | 
			
		||||
		Style:     "solarized-dark",
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// mock a sheet
 | 
			
		||||
	s := Sheet{
 | 
			
		||||
		Text: "echo 'foo'",
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// colorize the sheet text
 | 
			
		||||
	s.Colorize(conf)
 | 
			
		||||
 | 
			
		||||
	// initialize expectations
 | 
			
		||||
	want := "[38;2;181;137;0mecho[0m[38;2;147;161;161m"
 | 
			
		||||
	want += " [0m[38;2;42;161;152m'foo'[0m"
 | 
			
		||||
 | 
			
		||||
	// assert
 | 
			
		||||
	if s.Text != want {
 | 
			
		||||
		t.Errorf("failed to colorize sheet: want: %s, got: %s", want, s.Text)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@@ -69,3 +69,19 @@ func TestSheetFailure(t *testing.T) {
 | 
			
		||||
		t.Errorf("failed to return an error on unreadable sheet")
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// TestSheetFrontMatterFailure asserts that an error is returned if the sheet's
 | 
			
		||||
// frontmatter cannot be parsed.
 | 
			
		||||
func TestSheetFrontMatterFailure(t *testing.T) {
 | 
			
		||||
 | 
			
		||||
	// initialize a sheet
 | 
			
		||||
	_, err := New(
 | 
			
		||||
		"foo",
 | 
			
		||||
		mock.Path("sheet/bad-fm"),
 | 
			
		||||
		[]string{"alpha", "bravo"},
 | 
			
		||||
		false,
 | 
			
		||||
	)
 | 
			
		||||
	if err == nil {
 | 
			
		||||
		t.Errorf("failed to return an error on malformed front-matter")
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										4
									
								
								mocks/sheet/bad-fm
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								mocks/sheet/bad-fm
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,4 @@
 | 
			
		||||
---
 | 
			
		||||
syntax: sh
 | 
			
		||||
 | 
			
		||||
This is malformed frontmatter.
 | 
			
		||||
		Reference in New Issue
	
	Block a user