-
Notifications
You must be signed in to change notification settings - Fork 1
feat: added test and workflow that verifies templates #32
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b99ae07
feat: added test and workflow that verifies all templates start with …
RafaelJohn9 3e3b2ff
feat: updated the tests to capture all files\n\nUpdated the tests to …
RafaelJohn9 c905276
fix: added red triangle to easily spot the file
RafaelJohn9 c2ea0cf
fix: added comment to the first line of all our templates that were m…
RafaelJohn9 ff7f599
Update tests/verify_templates.rs
RafaelJohn9 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| name: Verify Templates | ||
|
|
||
| on: | ||
| pull_request: | ||
|
|
||
| jobs: | ||
| verify-templates: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - name: Set up Rust | ||
| uses: actions-rs/toolchain@v1 | ||
| with: | ||
| toolchain: stable | ||
| override: true | ||
| - name: Run verify-templates test | ||
| run: cargo test --test verify_templates |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| use std::collections::HashMap; | ||
| use std::fs; | ||
| use std::io::{BufRead, BufReader}; | ||
| use std::path::Path; | ||
|
|
||
| // Checks that all template files in the `templates` directory start with a comment | ||
| // appropriate for their file type. This is useful for ensuring that templates are well-documented | ||
| // and provide clear instructions or context for users who create new repositories from these templates. | ||
| #[test] | ||
| fn all_templates_start_with_comment() { | ||
| let template_dir = Path::new("./templates"); | ||
|
|
||
| // Map extensions to comment symbols | ||
| let comment_map = HashMap::from([ | ||
| ("rs", "//"), | ||
| ("py", "#"), | ||
| ("sh", "#"), | ||
| ("js", "//"), | ||
| ("ts", "//"), | ||
| ("html", "<!--"), | ||
| ("css", "/*"), | ||
| ("yaml", "#"), | ||
| ("yml", "#"), | ||
| ("toml", "#"), | ||
| ("md", "<!--"), | ||
| ]); | ||
|
|
||
| assert!( | ||
| template_dir.exists(), | ||
| "Template directory does not exist: {:?}", | ||
| template_dir | ||
| ); | ||
|
|
||
| fn check_templates_in_dir( | ||
| dir: &Path, | ||
| comment_map: &HashMap<&str, &str>, | ||
| failed_files: &mut Vec<(String, String, String)>, | ||
| ) { | ||
| for entry in fs::read_dir(dir).expect("Failed to read templates directory") { | ||
| let entry = entry.expect("Failed to read file entry"); | ||
| let path = entry.path(); | ||
|
|
||
| if path.is_dir() { | ||
| check_templates_in_dir(&path, comment_map, failed_files); | ||
| } else if path.is_file() { | ||
| let extension = path.extension().and_then(|s| s.to_str()).unwrap_or(""); | ||
| let comment_prefix = comment_map.get(extension).unwrap_or(&"#"); | ||
|
|
||
| let file = fs::File::open(&path).expect("Failed to open template file"); | ||
| let mut reader = BufReader::new(file); | ||
| let mut first_line = String::new(); | ||
|
|
||
| reader | ||
| .read_line(&mut first_line) | ||
| .expect("Failed to read first line"); | ||
|
|
||
| let trimmed = first_line.trim_start(); | ||
|
|
||
| let starts_with_comment = trimmed.starts_with(comment_prefix); | ||
| if !starts_with_comment { | ||
| failed_files.push(( | ||
| path.display().to_string(), | ||
| comment_prefix.to_string(), | ||
| first_line.clone(), | ||
| )); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| let mut failed_files = Vec::new(); | ||
| check_templates_in_dir(template_dir, &comment_map, &mut failed_files); | ||
|
|
||
| if !failed_files.is_empty() { | ||
| println!("\n❌ The following files do not start with the expected comment:"); | ||
| for (file, expected, first_line) in &failed_files { | ||
| println!( | ||
| "🔺 File: {}\n Expected prefix: {:?}\n First line: {}\n", | ||
| file, | ||
| expected, | ||
| first_line.trim_end() | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| assert!( | ||
| failed_files.is_empty(), | ||
| "Some template files do not start with the expected comment. See output above." | ||
| ); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.