1
1
package testutil
2
2
3
3
import (
4
+ "log"
4
5
"net/url"
5
6
"os"
6
7
"path/filepath"
@@ -18,25 +19,36 @@ func FormatSnapshots(
18
19
index * scip.Index ,
19
20
commentSyntax string ,
20
21
symbolFormatter scip.SymbolFormatter ,
22
+ customProjectRoot string ,
21
23
) ([]* scip.SourceFile , error ) {
22
24
var result []* scip.SourceFile
23
- projectRoot , err := url .Parse (index .Metadata .ProjectRoot )
25
+ projectRootUrl , err := url .Parse (index .Metadata .ProjectRoot )
24
26
if err != nil {
25
27
return nil , err
26
28
}
27
29
30
+ localSourcesRoot := projectRootUrl .Path
31
+ if customProjectRoot != "" {
32
+ localSourcesRoot = customProjectRoot
33
+ } else if _ , err := os .Stat (localSourcesRoot ); errors .Is (err , os .ErrNotExist ) {
34
+ cwd , _ := os .Getwd ()
35
+ log .Printf ("Project root [%s] doesn't exist, using current working directory [%s] instead. " +
36
+ "To override this behaviour, use --root=<folder> option directly" , projectRootUrl .Path , cwd )
37
+ localSourcesRoot = cwd
38
+ }
39
+
28
40
var documentErrors error
29
41
for _ , document := range index .Documents {
30
- snapshot , err := FormatSnapshot (document , index , commentSyntax , symbolFormatter )
42
+ sourceFilePath := filepath .Join (localSourcesRoot , document .RelativePath )
43
+ snapshot , err := FormatSnapshot (document , index , commentSyntax , symbolFormatter , sourceFilePath )
31
44
err = symbolFormatter .OnError (err )
32
45
if err != nil {
33
46
documentErrors = errors .CombineErrors (
34
47
documentErrors ,
35
48
errors .Wrap (err , document .RelativePath ),
36
49
)
37
50
}
38
- sourceFile := scip .NewSourceFile (
39
- filepath .Join (projectRoot .Path , document .RelativePath ),
51
+ sourceFile := scip .NewSourceFile (sourceFilePath ,
40
52
document .RelativePath ,
41
53
snapshot ,
42
54
)
@@ -55,16 +67,10 @@ func FormatSnapshot(
55
67
index * scip.Index ,
56
68
commentSyntax string ,
57
69
formatter scip.SymbolFormatter ,
70
+ sourceFilePath string ,
58
71
) (string , error ) {
59
72
b := strings.Builder {}
60
- uri , err := url .Parse (filepath .Join (index .Metadata .ProjectRoot , document .RelativePath ))
61
- if err != nil {
62
- return "" , err
63
- }
64
- if uri .Scheme != "file" {
65
- return "" , errors .New ("expected url scheme 'file', obtained " + uri .Scheme )
66
- }
67
- data , err := os .ReadFile (uri .Path )
73
+ data , err := os .ReadFile (sourceFilePath )
68
74
if err != nil {
69
75
return "" , err
70
76
}
0 commit comments