Skip to content

Commit

Permalink
Calculate removed and added lines separate and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rehlma committed Apr 30, 2024
1 parent 2e328df commit 414fafd
Show file tree
Hide file tree
Showing 6 changed files with 331 additions and 47 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
uses: actions/checkout@v4

- name: Check for Todos
uses: phntmxyz/pr_todo_checker@v1
uses: phntmxyz/pr_todo_checker@latest
with:
token: ${{ secrets.GITHUB_TOKEN }}
```
Expand All @@ -60,7 +60,7 @@ steps:
uses: actions/checkout@v4
- name: Check for Todos
uses: phntmxyz/pr_todo_checker@v1
uses: phntmxyz/pr_todo_checker@latest
with:
token: ${{ secrets.GITHUB_TOKEN }}
exclude: |
Expand Down
122 changes: 118 additions & 4 deletions __test__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { findTodos, generateComment } from '../src/tools'
import { Todo } from '../src/types'
import { excludeFilesPrDiff, todoPrDiff } from './test_data'
import {
excludeFilesDiff,
mixedTodoDiff,
newFileTodoDiff,
startWithRemovedLineTodoDiff,
updateTodoDiff
} from './test_data'

describe('extractTodos', () => {
it('should correctly extract todos', () => {
const fileTodos = findTodos(todoPrDiff)
it('should correctly extract mixed todos', () => {
const fileTodos = findTodos(mixedTodoDiff)

const expectedTodos: Todo[] = [
{
Expand Down Expand Up @@ -47,10 +53,118 @@ describe('extractTodos', () => {
expect(fileTodos).toEqual(expectedTodos)
})

it('should correctly extract updated todos', () => {
const fileTodos = findTodos(updateTodoDiff)

const expectedTodos: Todo[] = [
{
filename: 'README.md',
line: 29,
content: 'todo remved',
isAdded: false
},
{
filename: 'README.md',
line: 29,
content: 'todo updated',
isAdded: true
}
]
expect(fileTodos).toEqual(expectedTodos)
})

it('should correctly extract todos from a new file', () => {
const fileTodos = findTodos(newFileTodoDiff)

const expectedTodos: Todo[] = [
{
filename: 'first.js',
line: 3,
content: 'TODO first todo',
isAdded: true
},
{
filename: 'first.js',
line: 4,
content: 'TODO second todo',
isAdded: true
},
{
filename: 'first.js',
line: 5,
content: 'TODO third todo',
isAdded: true
},
{
filename: 'first.js',
line: 6,
content: 'TODO fourth todo',
isAdded: true
},
{
filename: 'first.js',
line: 14,
content: 'TODO: Implement methodA',
isAdded: true
},
{
filename: 'first.js',
line: 25,
content: 'TODO: Implement methodB',
isAdded: true
}
]
expect(fileTodos).toEqual(expectedTodos)
})

it('should correctly extract todos when diff starts with removed lines', () => {
const fileTodos = findTodos(startWithRemovedLineTodoDiff)

const expectedTodos: Todo[] = [
{
filename: 'first.js',
line: 4,
content: 'TODO first todo',
isAdded: false
},
{
filename: 'first.js',
line: 5,
content: 'TODO second todo',
isAdded: false
},
{
filename: 'first.js',
line: 6,
content: 'TODO third todo',
isAdded: false
},
{
filename: 'first.js',
line: 7,
content: 'TODO fourth todo',
isAdded: false
},
{
filename: 'first.js',
line: 15,
content: 'TODO: Implement methodA',
isAdded: true
},
{
filename: 'first.js',
line: 25,
content: 'TODO: Implement methodB',
isAdded: false
}
]
expect(fileTodos).toEqual(expectedTodos)
})

it('should correctly exclude files', () => {
const exclude = ['**/*.yml', '**/excluded/*']

const fileTodos = findTodos(excludeFilesPrDiff, exclude)
const fileTodos = findTodos(excludeFilesDiff, exclude)

const expectedTodos: Todo[] = [
{
Expand Down
117 changes: 110 additions & 7 deletions __test__/test_data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PrDiff } from '../src/types'

export const todoPrDiff: PrDiff = [
export const mixedTodoDiff: PrDiff = [
{
sha: '111111111',
filename: 'README.md',
Expand All @@ -16,7 +16,6 @@ export const todoPrDiff: PrDiff = [
- [Tool_A](https://example.com)
- [Tool_B](https://example.com)
-- [Tool_C](https://example.net)
\\ No newline at end of file
+- [Tool_C](https://example.com)
+
+- // TODO here`
Expand Down Expand Up @@ -70,7 +69,111 @@ export const todoPrDiff: PrDiff = [
}
]

export const excludeFilesPrDiff: PrDiff = [
export const updateTodoDiff: PrDiff = [
{
sha: '111111111',
filename: 'README.md',
status: 'modified',
additions: 3,
deletions: 1,
changes: 4,
blob_url: '',
raw_url: '',
contents_url: '',
patch: `@@ -26,4 +26,6 @@ TODO:
First line
- [Tool_A](https://example.com)
- [Tool_B](https://example.com)
-- // todo remved
+- // todo updated`
}
]

export const newFileTodoDiff: PrDiff = [
{
sha: '111111111',
filename: 'first.js',
status: 'added',
additions: 27,
deletions: 0,
changes: 0,
blob_url: '',
raw_url: '',
contents_url: '',
patch: `@@ -0,0 +1,27 @@
+// import second.js
+
+// TODO first todo
+// TODO second todo
+// TODO third todo
+// TODO fourth todo
+// Dummy class A
+class A {
+ constructor() {
+ this.propertyA = 'Value A';
+ }
+
+ methodA() {
+ // TODO: Implement methodA
+ }
+}
+
+// Dummy class B
+class B {
+ constructor() {
+ this.propertyB = 'Value B';
+ }
+
+ methodB() {
+ // TODO: Implement methodB
+ }
+}`
}
]

export const startWithRemovedLineTodoDiff: PrDiff = [
{
sha: '111111111',
filename: 'first.js',
status: 'added',
additions: 27,
deletions: 0,
changes: 0,
blob_url: '',
raw_url: '',
contents_url: '',
patch: `@@ -2,0 +8,27 @@
// import second.js
-
-// TODO first todo
-// TODO second todo
-// TODO third todo
-// TODO fourth todo
-// Dummy class A
class A {
constructor() {
this.propertyA = 'Value A';
}
methodA() {
+ // TODO: Implement methodA
}
}
// Dummy class B
-class B {
- constructor() {
- this.propertyB = 'Value B';
- }
-
- methodB() {
- // TODO: Implement methodB
- }
-}`
}
]

export const excludeFilesDiff: PrDiff = [
{
sha: 'sha',
filename: 'filename.js',
Expand All @@ -81,7 +184,7 @@ export const excludeFilesPrDiff: PrDiff = [
blob_url: 'blob_url',
raw_url: 'raw_url',
contents_url: 'contents_url',
patch: `@@ -22,6 +22,14 @@ any text';
patch: `@@ -0,0 +22,14 @@ any text';
+ // TODO - in filename js`,
previous_filename: undefined
},
Expand All @@ -95,7 +198,7 @@ export const excludeFilesPrDiff: PrDiff = [
blob_url: 'blob_url',
raw_url: 'raw_url',
contents_url: 'contents_url',
patch: `@@ -22,6 +22,14 @@ any text';
patch: `@@ -0,0 +22,14 @@ any text';
+ // TODO - in filename yml`,
previous_filename: undefined
},
Expand All @@ -109,7 +212,7 @@ export const excludeFilesPrDiff: PrDiff = [
blob_url: 'blob_url',
raw_url: 'raw_url',
contents_url: 'contents_url',
patch: `@@ -22,6 +22,14 @@ any text';
patch: `@@ -0,0 +22,14 @@ any text';
+ // TODO - in excluded directory`,
previous_filename: undefined
},
Expand All @@ -123,7 +226,7 @@ export const excludeFilesPrDiff: PrDiff = [
blob_url: 'blob_url',
raw_url: 'raw_url',
contents_url: 'contents_url',
patch: `@@ -22,6 +22,14 @@ any text';
patch: `@@ -0,0 +22,14 @@ any text';
+ // TODO - in included directory`,
previous_filename: undefined
}
Expand Down
Loading

0 comments on commit 414fafd

Please sign in to comment.