test: refactor fixtures

This commit is contained in:
Hiroki Osame
2023-05-03 22:55:58 +09:00
parent 9a841d946f
commit eda3462fec
15 changed files with 20 additions and 29 deletions

View File

@@ -1,11 +1,9 @@
import { readFile } from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';
import { expect, testSuite } from 'manten';
import {
generateCommitMessage,
} from '../../../src/utils/openai.js';
import type { ValidConfig } from '../../../src/utils/config.js';
import { getDiff } from '../../utils.js';
const { OPENAI_KEY } = process.env;
@@ -19,7 +17,7 @@ export default testSuite(({ describe }) => {
await test('Should not translate conventional commit type to Japanase when locale config is set to japanese', async () => {
const japaneseConventionalCommitPattern = /(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\(.*\))?: [\u3000-\u303F\u3040-\u309F\u30A0-\u30FF\uFF00-\uFF9F\u4E00-\u9FAF\u3400-\u4DBF]/;
const gitDiff = await readDiffFromFile('new-feature.txt');
const gitDiff = await getDiff('new-feature.diff');
const commitMessage = await runGenerateCommitMessage(gitDiff, {
locale: 'ja',
@@ -30,7 +28,7 @@ export default testSuite(({ describe }) => {
});
await test('Should use "feat:" conventional commit when change relate to adding a new feature', async () => {
const gitDiff = await readDiffFromFile('new-feature.txt');
const gitDiff = await getDiff('new-feature.diff');
const commitMessage = await runGenerateCommitMessage(gitDiff);
@@ -40,7 +38,7 @@ export default testSuite(({ describe }) => {
});
await test('Should use "refactor:" conventional commit when change relate to code refactoring', async () => {
const gitDiff = await readDiffFromFile('code-refactoring.txt');
const gitDiff = await getDiff('code-refactoring.diff');
const commitMessage = await runGenerateCommitMessage(gitDiff);
@@ -50,7 +48,7 @@ export default testSuite(({ describe }) => {
});
await test('Should use "test:" conventional commit when change relate to testing a React application', async () => {
const gitDiff = await readDiffFromFile('testing-react-application.txt');
const gitDiff = await getDiff('testing-react-application.diff');
const commitMessage = await runGenerateCommitMessage(gitDiff);
@@ -60,8 +58,8 @@ export default testSuite(({ describe }) => {
});
await test('Should use "build:" conventional commit when change relate to github action build pipeline', async () => {
const gitDiff = await readDiffFromFile(
'github-action-build-pipeline.txt',
const gitDiff = await getDiff(
'github-action-build-pipeline.diff',
);
const commitMessage = await runGenerateCommitMessage(gitDiff);
@@ -72,7 +70,7 @@ export default testSuite(({ describe }) => {
});
await test('Should use "(ci|build):" conventional commit when change relate to continious integration', async () => {
const gitDiff = await readDiffFromFile('continous-integration.txt');
const gitDiff = await getDiff('continous-integration.diff');
const commitMessage = await runGenerateCommitMessage(gitDiff);
@@ -83,7 +81,7 @@ export default testSuite(({ describe }) => {
});
await test('Should use "docs:" conventional commit when change relate to documentation changes', async () => {
const gitDiff = await readDiffFromFile('documentation-changes.txt');
const gitDiff = await getDiff('documentation-changes.diff');
const commitMessage = await runGenerateCommitMessage(gitDiff);
// should match "docs:" or "docs(<scope>):"
@@ -92,7 +90,7 @@ export default testSuite(({ describe }) => {
});
await test('Should use "fix:" conventional commit when change relate to fixing code', async () => {
const gitDiff = await readDiffFromFile('fix-nullpointer-exception.txt');
const gitDiff = await getDiff('fix-nullpointer-exception.diff');
const commitMessage = await runGenerateCommitMessage(gitDiff);
// should match "fix:" or "fix(<scope>):"
@@ -102,7 +100,7 @@ export default testSuite(({ describe }) => {
});
await test('Should use "style:" conventional commit when change relate to code style improvements', async () => {
const gitDiff = await readDiffFromFile('code-style.txt');
const gitDiff = await getDiff('code-style.diff');
const commitMessage = await runGenerateCommitMessage(gitDiff);
// should match "style:" or "style(<style>):"
@@ -111,7 +109,7 @@ export default testSuite(({ describe }) => {
});
await test('Should use "chore:" conventional commit when change relate to a chore or maintenance', async () => {
const gitDiff = await readDiffFromFile('chore.txt');
const gitDiff = await getDiff('chore.diff');
const commitMessage = await runGenerateCommitMessage(gitDiff);
// should match "chore:" or "chore(<style>):"
@@ -121,7 +119,7 @@ export default testSuite(({ describe }) => {
});
await test('Should use "perf:" conventional commit when change relate to a performance improvement', async () => {
const gitDiff = await readDiffFromFile('performance-improvement.txt');
const gitDiff = await getDiff('performance-improvement.diff');
const commitMessage = await runGenerateCommitMessage(gitDiff);
// should match "perf:" or "perf(<style>):"
@@ -143,19 +141,5 @@ export default testSuite(({ describe }) => {
return commitMessages[0];
}
/*
* See ./diffs/README.md in order to generate diff files
*/
async function readDiffFromFile(filename: string): Promise<string> {
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const gitDiff = await readFile(
path.resolve(__dirname, `./diff-fixtures/${filename}`),
'utf8',
);
return gitDiff;
}
});
});

View File

@@ -1,11 +0,0 @@
# Generating diffs
1. Instruct ChatGPT with the following command:
```
I want you to act as a git cli
I will give you the type of content and you will generate a random git diff based on that
```
2. Insert the type of change
ChatGPT will generate a fictional git diff based on the type of change you inserted.

View File

@@ -1,18 +0,0 @@
diff --git a/package.json b/package.json
index 2a7398e..6b2a3f0 100644
--- a/package.json
+++ b/package.json
@@ -3,7 +3,7 @@
"version": "1.0.0",
"description": "A sample project",
"main": "index.js",
- "scripts": {
+ "scripts": {
"start": "node index.js",
"test": "mocha",
- "lint": "eslint ."
+ "lint": "eslint .",
+ "clean": "rm -rf node_modules && npm install"
},
"dependencies": {
"express": "^4.17.1",

View File

@@ -1,34 +0,0 @@
diff --git a/old_example.ts b/new_example.ts
index 1234567..abcdefg 100644
--- a/old_example.ts
+++ b/new_example.ts
@@ -1,15 +1,16 @@
-import { Component, OnInit } from '@angular/core';
+import { Component } from '@angular/core';
-@Component({
- selector: 'app-example',
- templateUrl: './example.component.html',
- styleUrls: ['./example.component.css']
-})
-export class ExampleComponent implements OnInit {
- message: string;
+@Component({
+ selector: 'app-improved-example',
+ templateUrl: './improved-example.component.html',
+ styleUrls: ['./improved-example.component.css']
+})
+export class ImprovedExampleComponent {
+ private _message: string;
- ngOnInit() {
- this.message = 'Hello, world!';
+ constructor() {
+ this._message = 'Hello, world!';
}
+ get message(): string {
+ return this._message;
+ }
}

View File

@@ -1,28 +0,0 @@
diff --git a/src/app.js b/src/app.js
index 8741c37..91b2e74 100644
--- a/src/app.js
+++ b/src/app.js
@@ -10,12 +10,12 @@ app.use(express.json());
// Routes
app.get('/', (req, res) => {
- res.send('Welcome to the API!');
+ res.send('Welcome to the API!');
});
app.post('/users', (req, res) => {
- const user = createUser(req.body);
- res.status(201).send(user);
+ const user = createUser(req.body);
+ res.status(201).send(user);
});
app.get('/users/:id', (req, res) => {
@@ -27,7 +27,7 @@ app.get('/users/:id', (req, res) => {
if (user) {
res.send(user);
} else {
- res.status(404).send('User not found');
+ res.status(404).send('User not found');
}
});

View File

@@ -1,30 +0,0 @@
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
new file mode 100644
index 0000000..b6e5789
--- /dev/null
+++ b/.github/workflows/ci.yml
@@ -0,0 +1,16 @@
+name: Continuous Integration
+
+on:
+ push:
+ branches:
+ - main
+ pull_request:
+ branches:
+ - main
+
+jobs:
+ build-and-test:
+ runs-on: ubuntu-latest
+ steps:
+ - name: Checkout repository
+ uses: actions/checkout@v2
+ - name: Set up Node.js
+ uses: actions/setup-node@v2
+ with:
+ node-version: '16'
+ - name: Install dependencies
+ run: npm ci
+ - name: Run tests
+ run: npm test

View File

@@ -1,27 +0,0 @@
diff --git a/old_feature.py b/old_feature.py
index 1234567..abcdefg 100644
--- a/old_feature.py
+++ b/old_feature.py
@@ -1,7 +1,9 @@
import warnings
class OldFeature:
+ def __init__(self):
+ warnings.warn("OldFeature is deprecated and will be removed in the next release. Please use NewFeature instead.", DeprecationWarning)
def do_something(self):
print("Doing something with the old feature...")
diff --git a/new_feature.py b/new_feature.py
new file mode 100644
index 0000000..1111111
--- /dev/null
+++ b/new_feature.py
@@ -0,0 +1,7 @@
+class NewFeature:
+ def __init__(self):
+ print("Initializing the new feature...")
+
+ def do_something(self):
+ print("Doing something with the new feature...")
+

View File

@@ -1,30 +0,0 @@
diff --git a/README.md b/README.md
index a0c3e1b..9d1b6f8 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,11 @@
# My Awesome Project
+## Overview
+
+My Awesome Project is a web application that allows users to manage their tasks and projects in a simple and intuitive way. The project is built with React and Node.js and uses MongoDB for data storage.
+
## Installation
+To install and run My Awesome Project, follow these steps:
+
1. Clone the repository: `git clone https://github.com/username/my-awesome-project.git`
2. Install dependencies: `npm install`
3. Start the development server: `npm start`
@@ -13,6 +18,11 @@ To install and run My Awesome Project, follow these steps:
## Usage
To use My Awesome Project, follow these steps:
+
+1. Open your web browser and navigate to `http://localhost:3000`
+2. Sign up for a new account or log in to an existing one
+3. Create a new task or project and start managing your work!
+
## Contributing
We welcome contributions from anyone and everyone. To contribute to My Awesome Project, follow these steps:

View File

@@ -1,14 +0,0 @@
diff --git a/src/main/java/com/example/MyClass.java b/src/main/java/com/example/MyClass.java
index e7d8f38..caab7f1 100644
--- a/src/main/java/com/example/MyClass.java
+++ b/src/main/java/com/example/MyClass.java
@@ -23,7 +23,10 @@ public class MyClass {
public void processItems(List<Item> items) {
for (Item item : items) {
- if (item.getValue().equalsIgnoreCase("example")) {
+ // Fixing NullPointerException by adding a null check
+ String itemValue = item.getValue();
+ if (itemValue != null && itemValue.equalsIgnoreCase("example")) {
processExampleItem(item);
}
}

View File

@@ -1,21 +0,0 @@
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 1d07d31..085eb64 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -10,6 +10,8 @@ jobs:
- uses: actions/setup-node@v1
with:
node-version: 12.x
+ - name: Install dependencies
+ run: npm install
- name: Build and test
run: |
npm run build
@@ -22,3 +24,7 @@ jobs:
if: always()
uses: actions/upload-artifact@v1
with:
+ name: Build artifact
+ path: build
+ - name: Deploy to production
+ uses: some-third-party/deploy-action@v1

View File

@@ -1,47 +0,0 @@
diff --git a/src/features/newFeature.js b/src/features/newFeature.js
new file mode 100644
index 0000000..b6e5789
--- /dev/null
+++ b/src/features/newFeature.js
@@ -0,0 +1,18 @@
+/**
+ * New feature: Calculates the factorial of a given number.
+ * @param {number} n - The input number.
+ * @returns {number} - The factorial of the input number.
+ */
+function factorial(n) {
+ if (n === 0 || n === 1) {
+ return 1;
+ }
+ return n * factorial(n - 1);
+}
+
+module.exports = {
+ factorial,
+};
+
diff --git a/src/app.js b/src/app.js
index 8741c37..91b2e74 100644
--- a/src/app.js
+++ b/src/app.js
@@ -2,6 +2,7 @@
const express = require('express');
const bodyParser = require('body-parser');
const userRoutes = require('./routes/userRoutes');
+const { factorial } = require('./features/newFeature');
const app = express();
app.use(bodyParser.json());
@@ -21,6 +22,12 @@
res.send('Welcome to the API!');
});
+app.get('/factorial/:number', (req, res) => {
+ const number = parseInt(req.params.number, 10);
+ const result = factorial(number);
+ res.send(`Factorial of ${number} is ${result}`);
+});
+
// Other routes...
module.exports = app;

View File

@@ -1,26 +0,0 @@
diff --git a/src/loop.js b/src/loop.js
index 1d45a2b..8c52e81 100644
--- a/src/loop.js
+++ b/src/loop.js
@@ -5,14 +5,14 @@ const items = generateItems(100000);
function processData(items) {
let sum = 0;
- for (let i = 0; i < items.length; i++) {
- const item = items[i];
- if (item.isValid()) {
- sum += item.value;
- }
+ for (const item of items) {
+ if (item.isValid()) sum += item.value;
}
return sum;
}
const startTime = Date.now();
-const result = processData(items);
+const result = processData(items); // Improved loop iteration
const endTime = Date.now();
console.log(`Result: ${result}, Time: ${endTime - startTime} ms`);

View File

@@ -1,27 +0,0 @@
diff --git a/Controllers/FeatureController.cs b/Controllers/FeatureController.cs
index 8a3b7c1..3e29f9a 100644
--- a/Controllers/FeatureController.cs
+++ b/Controllers/FeatureController.cs
@@ -1,16 +1,7 @@
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
namespace MyWebApi.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class FeatureController : ControllerBase
{
- [HttpGet("old-feature")]
- public ActionResult<string> GetOldFeature()
- {
- return "This is the removed old feature.";
- }
-
[HttpGet("new-feature")]
public ActionResult<string> GetNewFeature()
{
return "This is the new feature.";
}
}
}

View File

@@ -1,22 +0,0 @@
diff --git a/src/components/MyComponent.test.js b/src/components/MyComponent.test.js
index 37eabf2..976c6bf 100644
--- a/src/components/MyComponent.test.js
+++ b/src/components/MyComponent.test.js
@@ -10,6 +10,7 @@ describe("MyComponent", () => {
});
it("renders the component correctly", () => {
+ const props = { name: "John Doe", age: 25 };
const tree = renderer.create(<MyComponent {...props} />).toJSON();
expect(tree).toMatchSnapshot();
});
@@ -25,6 +26,11 @@ describe("MyComponent", () => {
expect(wrapper.find("h1").text()).toEqual("Hello, John Doe!");
});
+ it("displays the correct age", () => {
+ const props = { name: "Jane Doe", age: 30 };
+ const wrapper = shallow(<MyComponent {...props} />);
+ expect(wrapper.find("p").text()).toEqual("Age: 30");
+ });
});