Skip to content
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

Unique File Tags #7

Merged
merged 2 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions __tests__/testData/recursionStart/sub/dummyDoc.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Dummy Doc

This file dummyDoc.md is for unit tests in __tests__/ like e.g. fakeName.cpp or main.test.ts.
The file main.ts shall be mentioned here and below.
All tags that do not exist should contain 'fake' to help the unit tests.

## Related files and folders
Expand All @@ -9,3 +10,4 @@ All tags that do not exist should contain 'fake' to help the unit tests.
- utils.ts
- script/
- __tests__/testData/taggedFolder/
- script/
13 changes: 12 additions & 1 deletion __tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
*/

import {
assertNonNull,
getUrlToFile,
getUrlToChanges,
findFileByName,
assertNonNull
uniqueFilter
} from '../src/utils'
import * as github from '@actions/github'
import { expect } from '@jest/globals'
Expand Down Expand Up @@ -132,4 +133,14 @@ describe('utils.ts context dependent', () => {
// Calling the function with this context should throw an error.
expect(() => getUrlToChanges(filePath)).toThrow()
})

it('uniqueFilter(): Test functionality', () => {
const noDuplicates = [0, 1, 2, 3, 4]
const noDuplicatesFiltered = noDuplicates.filter(uniqueFilter)
expect(noDuplicatesFiltered).toEqual(noDuplicates)

const duplicates = [0, 1, 2, 2, 3, 4, 2]
const duplicatesFiltered = duplicates.filter(uniqueFilter)
expect(duplicatesFiltered).toEqual(noDuplicates)
})
})
2 changes: 1 addition & 1 deletion badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 18 additions & 5 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 10 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import {
assertNonNull,
findFileByName,
getUrlToChanges,
getUrlToFile
getUrlToFile,
uniqueFilter
} from './utils'

/**
* Checks if file extensions start with a '.' and then only consist of letters and numbers.
* @param extensions Array of extensions to check.
* @param extension The extensions to check.
* @return True if extension matches the sane pattern.
*/
function extensionsIsSane(extension: string): boolean {
Expand All @@ -35,7 +36,7 @@ function extractFileTags(
.map(f => f.substring(1, f.length))
.join('|')
const srcFileRegex = new RegExp(`[^/\\s]+\\.(${extensionsCombined})\\b`, 'g')
return Array.from(fileContent.match(srcFileRegex) || [])
return Array.from(fileContent.match(srcFileRegex)?.filter(uniqueFilter) || [])
}

/**
Expand All @@ -53,7 +54,8 @@ function extractDirectoryTags(fileContent: string): string[] {
// If there is nothing after the tag default to undefined and thus to []
// Else match anything that ends on a '/'
// \S = anything but whitespace
?.match(/[\S]+\/(?!\S)/g) || []
?.match(/[\S]+\/(?!\S)/g)
?.filter(uniqueFilter) || []
)
}

Expand Down Expand Up @@ -94,7 +96,7 @@ function checkDocumentation(
// Append the content of all directories to the tags.
for (const tag of directoryTags) {
if (!fs.existsSync(tag)) {
// If the dir can not be found it's an unknown tag.
// If the dir can not be found it's an unknown tag. Duplicates here are intentional.
unknownTagsLocal.push(tag)
} else {
// Read the content of the directory and split it in files and dirs.
Expand All @@ -113,6 +115,9 @@ function checkDocumentation(
}
}

// Make sure all tags are only listed once
fileTags = fileTags.filter(uniqueFilter)

// Analyze all file tags for doc changes.
for (const tag of fileTags) {
// If any tag appears in the changes, the doc file also has to be in the changes.
Expand Down
14 changes: 14 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@ import * as path from 'path'
import * as github from '@actions/github'
import crypto from 'crypto'

/**
* Filter function to remove duplicates in an array.
* @param value Value to check for duplication
* @param index Known index of first occurrence of value.
* @param container Array to filter.
*/
export function uniqueFilter<T>(
value: T,
index: number,
container: T[]
): boolean {
return container.indexOf(value) === index
}

/**
* Retrieves the value of an optional type.
* If value is undefined or null an Error is thrown.
Expand Down
Loading