| 
									
										
										
										
											2022-03-17 09:52:22 +00:00
										 |  |  | package util | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2022-04-06 08:17:36 +00:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2022-04-06 08:00:23 +00:00
										 |  |  | 	"os" | 
					
						
							|  |  |  | 	"os/user" | 
					
						
							| 
									
										
										
										
											2022-04-06 08:17:36 +00:00
										 |  |  | 	"path/filepath" | 
					
						
							|  |  |  | 	"runtime" | 
					
						
							| 
									
										
										
										
											2022-04-06 08:00:23 +00:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2022-03-17 09:52:22 +00:00
										 |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 07:43:17 +00:00
										 |  |  | 	"github.com/stretchr/testify/assert" | 
					
						
							| 
									
										
										
										
											2022-03-17 09:52:22 +00:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestFileExist(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2022-04-06 07:43:17 +00:00
										 |  |  | 	assert.True(t, FileExists("utils.go")) | 
					
						
							| 
									
										
										
										
											2022-04-06 08:00:23 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestFileExistNot(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2022-04-06 07:43:17 +00:00
										 |  |  | 	assert.True(t, !FileExists("Utils2.go")) | 
					
						
							| 
									
										
										
										
											2022-03-17 09:52:22 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 08:00:23 +00:00
										 |  |  | func TestJoiningSlash1(t *testing.T) { | 
					
						
							|  |  |  | 	actual := JoiningSlash("http://my.tld/docs/", "bla/", "blub/") | 
					
						
							|  |  |  | 	expected := "http://my.tld/docs/bla/blub/" | 
					
						
							|  |  |  | 	assert.Equal(t, expected, actual) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestJoiningSlash2(t *testing.T) { | 
					
						
							|  |  |  | 	actual := JoiningSlash("http://my.tld", "bla", "blub") | 
					
						
							|  |  |  | 	assert.Equal(t, "http://my.tld/bla/blub", actual) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestJoiningSlash3(t *testing.T) { | 
					
						
							|  |  |  | 	actual := JoiningSlash("http://my.tld/", "bla", "blub") | 
					
						
							|  |  |  | 	assert.Equal(t, "http://my.tld/bla/blub", actual) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | func TestJoiningSlash4(t *testing.T) { | 
					
						
							|  |  |  | 	actual := JoiningSlash("http://my.tld", "bla/", "blub") | 
					
						
							|  |  |  | 	assert.Equal(t, "http://my.tld/bla/blub", actual) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | func TestJoiningSlash5(t *testing.T) { | 
					
						
							|  |  |  | 	actual := JoiningSlash("http://my.tld/docs", "bla/", "blub") | 
					
						
							|  |  |  | 	expected := "http://my.tld/docs/bla/blub" | 
					
						
							|  |  |  | 	assert.Equal(t, expected, actual) | 
					
						
							| 
									
										
										
										
											2022-03-17 09:52:22 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2022-04-06 07:43:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 08:00:23 +00:00
										 |  |  | func TestJoiningSlash6(t *testing.T) { | 
					
						
							|  |  |  | 	actual := JoiningSlash("http://my.tld/docs/", "bla/", "blub") | 
					
						
							|  |  |  | 	expected := "http://my.tld/docs/bla/blub" | 
					
						
							|  |  |  | 	assert.Equal(t, expected, actual) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  | Can run only as admin within windows or linux | 
					
						
							|  |  |  | e.g. sudo TESTASSUDO=yes /usr/local/go/bin/go test -timeout 30s -run ^TestIsSuperUser$ | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2022-04-06 07:43:17 +00:00
										 |  |  | func TestIsSuperUser(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2022-04-06 08:00:23 +00:00
										 |  |  | 	if !strings.EqualFold(os.Getenv("TESTASSUDO"), "yes") { | 
					
						
							|  |  |  | 		t.Skip("Skipping in normal tests") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-04-06 07:43:17 +00:00
										 |  |  | 	cuser, err := user.Current() | 
					
						
							|  |  |  | 	assert.Nil(t, err) | 
					
						
							|  |  |  | 	assert.NotNil(t, cuser) | 
					
						
							|  |  |  | 	assert.True(t, IsSuperUser()) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-04-06 08:17:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestGlobalConfigurationDirectoryWindows(t *testing.T) { | 
					
						
							|  |  |  | 	if runtime.GOOS != "windows" { | 
					
						
							|  |  |  | 		t.Skip(fmt.Sprintf("Skipping on OS %s", runtime.GOOS)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	appFolder := GetGlobalConfigurationDirectory("myapp") | 
					
						
							|  |  |  | 	assert.Equal(t, filepath.Join(os.Getenv("APPDATA"), "myapp"), appFolder) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestGlobalConfigurationDirectoryLinux(t *testing.T) { | 
					
						
							|  |  |  | 	if runtime.GOOS != "linux" { | 
					
						
							|  |  |  | 		t.Skip(fmt.Sprintf("Skipping on OS %s", runtime.GOOS)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	appFolder := GetGlobalConfigurationDirectory("myapp") | 
					
						
							|  |  |  | 	assert.Equal(t, "/etc/myapp", appFolder) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestGlobalConfigurationDirectoryMacOS(t *testing.T) { | 
					
						
							|  |  |  | 	if runtime.GOOS != "darwin" { | 
					
						
							|  |  |  | 		t.Skip(fmt.Sprintf("Skipping on OS %s", runtime.GOOS)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	appFolder := GetGlobalConfigurationDirectory("myapp") | 
					
						
							|  |  |  | 	assert.Equal(t, "/etc/myapp", appFolder) | 
					
						
							|  |  |  | } |