mirror of
				https://github.com/cheat/cheat.git
				synced 2025-11-04 07:45:28 +01:00 
			
		
		
		
	feat(tests): add unit-tests
Add unit-tests for `sheets.Load`.
This commit is contained in:
		@@ -13,7 +13,7 @@ func Path(filename string) string {
 | 
			
		||||
	// determine the path of this file during runtime
 | 
			
		||||
	_, thisfile, _, _ := runtime.Caller(0)
 | 
			
		||||
 | 
			
		||||
	// compute the config path
 | 
			
		||||
	// compute the mock path
 | 
			
		||||
	file, err := filepath.Abs(
 | 
			
		||||
		path.Join(
 | 
			
		||||
			filepath.Dir(thisfile),
 | 
			
		||||
@@ -22,7 +22,7 @@ func Path(filename string) string {
 | 
			
		||||
		),
 | 
			
		||||
	)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		panic(fmt.Errorf("failed to resolve config path: %v", err))
 | 
			
		||||
		panic(fmt.Errorf("failed to resolve mock path: %v", err))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return file
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,62 @@
 | 
			
		||||
package sheets
 | 
			
		||||
 | 
			
		||||
// TODO
 | 
			
		||||
import (
 | 
			
		||||
	"path"
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
	"github.com/cheat/cheat/internal/cheatpath"
 | 
			
		||||
	"github.com/cheat/cheat/internal/mock"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// TestLoad asserts that sheets on valid cheatpaths can be loaded successfully
 | 
			
		||||
func TestLoad(t *testing.T) {
 | 
			
		||||
 | 
			
		||||
	// mock cheatpaths
 | 
			
		||||
	cheatpaths := []cheatpath.Cheatpath{
 | 
			
		||||
		{
 | 
			
		||||
			Name:     "community",
 | 
			
		||||
			Path:     path.Join(mock.Path("cheatsheets"), "community"),
 | 
			
		||||
			ReadOnly: true,
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			Name:     "personal",
 | 
			
		||||
			Path:     path.Join(mock.Path("cheatsheets"), "personal"),
 | 
			
		||||
			ReadOnly: false,
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// load cheatsheets
 | 
			
		||||
	sheets, err := Load(cheatpaths)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Errorf("failed to load cheatsheets: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// assert that the correct number of sheets loaded
 | 
			
		||||
	// (sheet load details are tested in `sheet_test.go`)
 | 
			
		||||
	want := 4
 | 
			
		||||
	if len(sheets) != want {
 | 
			
		||||
		t.Errorf(
 | 
			
		||||
			"failed to load correct number of cheatsheets: want: %d, got: %d",
 | 
			
		||||
			want,
 | 
			
		||||
			len(sheets),
 | 
			
		||||
		)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// TestLoadBadPath asserts that an error is returned if a cheatpath is invalid
 | 
			
		||||
func TestLoadBadPath(t *testing.T) {
 | 
			
		||||
 | 
			
		||||
	// mock a bad cheatpath
 | 
			
		||||
	cheatpaths := []cheatpath.Cheatpath{
 | 
			
		||||
		{
 | 
			
		||||
			Name:     "badpath",
 | 
			
		||||
			Path:     "/cheat/test/path/does/not/exist",
 | 
			
		||||
			ReadOnly: true,
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// attempt to load the cheatpath
 | 
			
		||||
	if _, err := Load(cheatpaths); err == nil {
 | 
			
		||||
		t.Errorf("failed to reject invalid cheatpath")
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										0
									
								
								mocks/cheatsheets/community/.hiddenfile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								mocks/cheatsheets/community/.hiddenfile
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										4
									
								
								mocks/cheatsheets/community/bar
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								mocks/cheatsheets/community/bar
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,4 @@
 | 
			
		||||
---
 | 
			
		||||
tags: [ community ]
 | 
			
		||||
---
 | 
			
		||||
This is the bar cheatsheet.
 | 
			
		||||
							
								
								
									
										4
									
								
								mocks/cheatsheets/community/foo
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								mocks/cheatsheets/community/foo
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,4 @@
 | 
			
		||||
---
 | 
			
		||||
tags: [ community ]
 | 
			
		||||
---
 | 
			
		||||
This is the foo cheatsheet.
 | 
			
		||||
							
								
								
									
										4
									
								
								mocks/cheatsheets/personal/bat
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								mocks/cheatsheets/personal/bat
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,4 @@
 | 
			
		||||
---
 | 
			
		||||
tags: [ personal ]
 | 
			
		||||
---
 | 
			
		||||
This is the bat cheatsheet.
 | 
			
		||||
							
								
								
									
										4
									
								
								mocks/cheatsheets/personal/baz
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								mocks/cheatsheets/personal/baz
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,4 @@
 | 
			
		||||
---
 | 
			
		||||
tags: [ personal ]
 | 
			
		||||
---
 | 
			
		||||
This is the baz cheatsheet.
 | 
			
		||||
		Reference in New Issue
	
	Block a user