| 
									
										
										
										
											2022-01-12 00:37:58 +08:00
										 |  |  | package util | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2023-12-19 18:01:48 +08:00
										 |  |  | 	"path/filepath" | 
					
						
							| 
									
										
										
										
											2022-01-12 00:37:58 +08:00
										 |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/stretchr/testify/assert" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestCleanRelativePath(t *testing.T) { | 
					
						
							|  |  |  | 	testcases := []struct { | 
					
						
							|  |  |  | 		input        string | 
					
						
							|  |  |  | 		expectedPath string | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input:        "", | 
					
						
							|  |  |  | 			expectedPath: ".", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2023-12-19 18:01:48 +08:00
										 |  |  | 			input:        filepath.Join(string(filepath.Separator), "test", "test.txt"), | 
					
						
							|  |  |  | 			expectedPath: filepath.Join("test", "test.txt"), | 
					
						
							| 
									
										
										
										
											2022-01-12 00:37:58 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2023-12-19 18:01:48 +08:00
										 |  |  | 			input:        filepath.Join("..", "..", "test", "test.txt"), | 
					
						
							|  |  |  | 			expectedPath: filepath.Join("test", "test.txt"), | 
					
						
							| 
									
										
										
										
											2022-01-12 00:37:58 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2023-12-19 18:01:48 +08:00
										 |  |  | 			// since filepath.Join will remove the leading dot, we need to build the path manually
 | 
					
						
							|  |  |  | 			input:        "." + string(filepath.Separator) + filepath.Join("..", "test", "test.txt"), | 
					
						
							|  |  |  | 			expectedPath: filepath.Join("test", "test.txt"), | 
					
						
							| 
									
										
										
										
											2022-01-12 00:37:58 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, tt := range testcases { | 
					
						
							|  |  |  | 		path, err := CleanRelativePath(tt.input) | 
					
						
							|  |  |  | 		assert.NoError(t, err) | 
					
						
							|  |  |  | 		assert.Equal(t, tt.expectedPath, path) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |