-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
lsp/templating: gracefully handle unknown root #1171
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,14 +41,6 @@ func TestTemplateContentsForFile(t *testing.T) { | |
}, | ||
ExpectedError: "file on disk already has contents", | ||
}, | ||
"empty file is templated as main when no root": { | ||
FileKey: "foo/bar.rego", | ||
CacheFileContents: "", | ||
DiskContents: map[string]string{ | ||
"foo/bar.rego": "", | ||
}, | ||
ExpectedContents: "package main\n\nimport rego.v1\n", | ||
}, | ||
"empty file is templated based on root": { | ||
FileKey: "foo/bar.rego", | ||
CacheFileContents: "", | ||
|
@@ -100,7 +92,6 @@ func TestTemplateContentsForFile(t *testing.T) { | |
|
||
ctx := context.Background() | ||
|
||
// create a new language server | ||
s := NewLanguageServer(ctx, &LanguageServerOptions{ErrorLog: newTestLogger(t)}) | ||
s.workspaceRootURI = uri.FromPath(clients.IdentifierGeneric, td) | ||
|
||
|
@@ -124,6 +115,73 @@ func TestTemplateContentsForFile(t *testing.T) { | |
} | ||
} | ||
|
||
func TestTemplateContentsForFileInWorkspaceRoot(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Root is not allowed and is an error which can be silently ignored if the caller understands it. |
||
t.Parallel() | ||
|
||
td := t.TempDir() | ||
|
||
err := os.MkdirAll(filepath.Join(td, ".regal"), 0o755) | ||
if err != nil { | ||
t.Fatalf("failed to create directory %s: %s", filepath.Join(td, ".regal"), err) | ||
} | ||
|
||
err = os.WriteFile(filepath.Join(td, ".regal/config.yaml"), []byte{}, 0o600) | ||
if err != nil { | ||
t.Fatalf("failed to create file %s: %s", filepath.Join(td, ".regal"), err) | ||
} | ||
|
||
ctx := context.Background() | ||
|
||
s := NewLanguageServer(ctx, &LanguageServerOptions{ErrorLog: newTestLogger(t)}) | ||
s.workspaceRootURI = uri.FromPath(clients.IdentifierGeneric, td) | ||
|
||
fileURI := uri.FromPath(clients.IdentifierGeneric, filepath.Join(td, "foo.rego")) | ||
|
||
s.cache.SetFileContents(fileURI, "") | ||
|
||
_, err = s.templateContentsForFile(fileURI) | ||
if err == nil { | ||
t.Fatalf("expected error") | ||
} | ||
|
||
if !strings.Contains(err.Error(), "this function does not template files in the workspace root") { | ||
t.Fatalf("expected error about root templating, got %s", err.Error()) | ||
} | ||
} | ||
|
||
func TestTemplateContentsForFileWithUnknownRoot(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When the root is unknown, but there is a path, we can attempt to use the server's root URI as a root instead. |
||
t.Parallel() | ||
|
||
td := t.TempDir() | ||
|
||
ctx := context.Background() | ||
|
||
s := NewLanguageServer(ctx, &LanguageServerOptions{ErrorLog: newTestLogger(t)}) | ||
s.workspaceRootURI = uri.FromPath(clients.IdentifierGeneric, td) | ||
|
||
err := os.MkdirAll(filepath.Join(td, "foo"), 0o755) | ||
if err != nil { | ||
t.Fatalf("failed to create directory %s: %s", filepath.Join(td, "foo"), err) | ||
} | ||
|
||
fileURI := uri.FromPath(clients.IdentifierGeneric, filepath.Join(td, "foo/bar.rego")) | ||
|
||
s.cache.SetFileContents(fileURI, "") | ||
|
||
newContents, err := s.templateContentsForFile(fileURI) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %s", err) | ||
} | ||
|
||
exp := `package foo | ||
|
||
import rego.v1 | ||
` | ||
if exp != newContents { | ||
t.Errorf("unexpected content: %s, want %s", newContents, exp) | ||
} | ||
} | ||
|
||
func TestNewFileTemplating(t *testing.T) { | ||
t.Parallel() | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is unrelated, but is something that I noticed when working with empty files, a better check here is to test if there are modules rather than if there are files since empty files have no parse.