Skip to content

Add tests for template file syntax errors #3962

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

salmanmkc
Copy link
Contributor

@salmanmkc salmanmkc commented Jul 27, 2025

Introduce tests to ensure that template files do not contain syntax errors. This enhances code reliability and maintainability.

Using shellcheck for syntax checking for sh template files https://github.com/koalaman/shellcheck

Aims to avoid issues like this #3955

@Copilot Copilot AI review requested due to automatic review settings July 27, 2025 21:24
@salmanmkc salmanmkc requested a review from a team as a code owner July 27, 2025 21:24
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces comprehensive test coverage for shell script template files to prevent syntax errors from being introduced into production template files. The tests validate that template files have valid syntax by processing them with shell interpreters and checking for errors.

  • Adds a new test class ShellScriptSyntaxL0 with syntax validation tests for shell script templates
  • Includes both positive tests (valid syntax) and negative tests (intentionally broken syntax) to ensure the validation mechanism works correctly
  • Covers multiple template files including update scripts and service scripts for different platforms

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
src/Test/L0/Listener/ShellScriptSyntaxL0.cs New test class implementing comprehensive syntax validation for shell script and cmd templates
src/Misc/layoutbin/update.sh.template Minor formatting change splitting if-then statement across multiple lines

@salmanmkc salmanmkc marked this pull request as draft July 27, 2025 21:45
@salmanmkc salmanmkc marked this pull request as ready for review July 27, 2025 22:16
@salmanmkc salmanmkc marked this pull request as draft July 27, 2025 22:28
@salmanmkc salmanmkc marked this pull request as ready for review July 28, 2025 12:33
string templatePath = Path.Combine(tempDir, "complex_shell.sh.template");

// Write a sample template with various shell features
string template = @"#!/bin/bash
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are we testing with these? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is valuable to keep because it tests complex shell syntax patterns that might not be present in our actual template files. It serves as validation for our syntax checking framework itself, not just our current templates.

The test covers important shell script features like:

  • Nested quotes and complex quoting patterns
  • Function definitions with parameter handling
  • Command substitution and string manipulation
  • Here documents

These patterns might not all exist in our current templates, but having this test ensures our validation system can handle them if we ever add them. It's basically testing the validator itself rather than just the templates.

I'd vote to keep it as a solid regression test for our validation framework, in-case we modify it going forward.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just replace all the placeholder strings in the script and then run bash -n against it?
So we don't need to add any 3rd party dependency.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fr

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With just bash -n it wasn't able to catch all the syntax errors, I tried making various syntax errors and they would not get detected, missing brackets for example would not get detected at all

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, tests pass with missing bracket with bash -n and no shellcheck
test pass with missing bracket

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With shell check it gives an error
syntax error

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have it so that if the dependency isn't there, it just uses bash -n still though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants