mirror of
https://github.com/grafana/grafana.git
synced 2025-08-02 09:02:22 +08:00
Transformations: Add transformation builder tests and refactor (#83285)
* baldm0mma/script_tests/ update jest.config paths * baldm0mma/script_tests/ refactor makefile content * baldm0mma/script_tests/ refactor file write commands * baldm0mma/script_tests/ create test module * baldm0mma/script_tests/ add to codeowners * baldm0mma/script_tests/ recenter content * baldm0mma/script_tests/ create buildMarkdownContent and update whitespace * baldm0mma/script_tests/ run build script * baldm0mma/script_tests/ update tests to remove make dep and node dep * baldm0mma/script_tests/ update cntent * baldm0mma/script_tests/ update makefile * baldm0mma/script_tests/ update buildMarkdownContent * baldm0mma/script_tests/ remove unused imports * baldm0mma/script_tests/ update test annotation * baldm0mma/script_tests/ prettier * baldm0mma/script_tests/ update tests * baldm0mma/script_tests/ update content * baldm0mma/script_tests/ update annos * baldm0mma/script_tests/ remove unused vars * baldm0mma/script_tests/ remove unused imports * baldm0mma/script_tests/ update annos * baldm0mma/script_tests/ update paths * Update scripts/docs/generate-transformations.test.ts Co-authored-by: Jack Baldry <jack.baldry@grafana.com> * baldm0mma/script_tests/ update comment --------- Co-authored-by: Jack Baldry <jack.baldry@grafana.com>
This commit is contained in:
57
scripts/docs/generate-transformations.test.ts
Normal file
57
scripts/docs/generate-transformations.test.ts
Normal file
@ -0,0 +1,57 @@
|
||||
import { getMarkdownContent, getJavaScriptContent, readMeContent } from './generate-transformations.ts';
|
||||
|
||||
describe('makefile script tests', () => {
|
||||
it('should execute without error and match the content written to index.md', () => {
|
||||
// Normalize and compare.
|
||||
expect(contentDoesMatch(getJavaScriptContent(), getMarkdownContent())).toBe(true);
|
||||
});
|
||||
|
||||
it('should be able to tell if the content DOES NOT match', () => {
|
||||
const wrongContent = getJavaScriptContent().concat('additional content to mismatch');
|
||||
// Normalize and compare.
|
||||
expect(contentDoesMatch(wrongContent, getMarkdownContent())).toBe(false);
|
||||
|
||||
// If this test fails, refer to `./docs/README.md` "Content guidelines" for more information
|
||||
// about editing and building the Transformations docs.
|
||||
});
|
||||
});
|
||||
|
||||
export function contentDoesMatch(jsContent: string, mdContent: string): Boolean {
|
||||
return normalizeContent(jsContent) === normalizeContent(mdContent);
|
||||
}
|
||||
|
||||
/*
|
||||
Normalize content by removing all whitespace (spaces, tabs, newlines, carriage returns,
|
||||
form feeds, and vertical tabs) and special characters.
|
||||
|
||||
NOTE: There are numerous unpredictable formatting oddities when transforming JavaScript to Markdown;
|
||||
almost all of them are irrelevant to the actual content of the file, which is why we strip them out here.
|
||||
|
||||
For example:
|
||||
|
||||
In JavaScript, the following string table
|
||||
|
||||
| A | B | C |
|
||||
| - | - | - |
|
||||
| 1 | 3 | 5 |
|
||||
| 2 | 4 | 6 |
|
||||
| 3 | 5 | 7 |
|
||||
| 4 | 6 | 8 |
|
||||
| 5 | 7 | 9 |
|
||||
|
||||
parses to Markdown as
|
||||
|
||||
| A | B | C |
|
||||
| --- | --- | --- | <--------- notice the extra hyphens
|
||||
| 1 | 3 | 5 | <--------- notice the extra spaces
|
||||
| 2 | 4 | 6 |
|
||||
| 3 | 5 | 7 |
|
||||
| 4 | 6 | 8 |
|
||||
| 5 | 7 | 9 |
|
||||
|
||||
This is one of many arbitrary formatting anomalies that we can ignore by normalizing the
|
||||
content before comparing the JavaScript template literals and the final Markdown.
|
||||
*/
|
||||
function normalizeContent(content: string): string {
|
||||
return content.replace(/\s+|[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/g, '').trim();
|
||||
}
|
Reference in New Issue
Block a user