Skip to content

Unit tests for displayPath function #5676

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

torade
Copy link

@torade torade commented Jul 24, 2025

📚 Class Assignment: Adding Unit Tests

Assignment Context: This pull request fulfills a university requirement to add unit tests to an open-source project.

Description

Added unit tests for the displayPath utility function from .\lib\hexo\load_plugins.ts. The tests cover Unix and Windows paths, and base directory edge cases.

Changes

  • new file with the actual unit test at .\test\scripts\hexo\displayPath.ts
  • changed the displayPath function to be exported
  • modified module export in load_plugins.ts:
image

!! Known Issues !!

  • the module export modification breaks existing imports and causes some tests to fail.

Running tests

  • Unit test: npm test -- --grep "displayPath" -> PASS
  • all tests: npm test -> FAIL (some)

Note for maintainers: This is a learning exercise, and the changes proposed are currently deemed unsuitable for merging. Any constructive feedback and guidance on the proper approach is highly appreciated and welcomed.

Copy link

How to test

git clone -b add-displaypath-tests https://github.com/torade/hexo.git
cd hexo
npm install
npm test

@stevenjoezhang stevenjoezhang requested a review from Copilot July 24, 2025 10:45
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds unit tests for the displayPath utility function as part of a university assignment. The student acknowledges that the changes break existing imports and cause test failures due to changing the module export pattern from CommonJS to ES modules.

  • Added comprehensive unit tests covering Unix paths, Windows paths, and base directory edge cases
  • Modified the module export structure from export = to export default and named exports
  • Exported the previously internal displayPath function to enable testing

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
test/scripts/hexo/displayPath.ts New test file with unit tests for the displayPath function
lib/hexo/load_plugins.ts Changed module exports and made displayPath function exportable

@@ -0,0 +1,37 @@
//NEW UNIT TEST FOR displayPath FUNCTION
Copy link
Preview

Copilot AI Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Remove the all-caps comment. Use standard comment formatting or a proper file header if documentation is needed.

Suggested change
//NEW UNIT TEST FOR displayPath FUNCTION
// Unit tests for the displayPath function

Copilot uses AI. Check for mistakes.

@@ -77,6 +77,7 @@ function loadScripts(ctx: Hexo): Promise<void[][]> {
}));
}

function displayPath(path: string, baseDirLength: number): string {
//EXPORTING displayPath FUNCTION FOR TESTING
Copy link
Preview

Copilot AI Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Remove the all-caps comment. The export statement is self-documenting.

Suggested change
//EXPORTING displayPath FUNCTION FOR TESTING

Copilot uses AI. Check for mistakes.

@@ -4,7 +4,7 @@ import Promise from 'bluebird';
import { magenta } from 'picocolors';
import type Hexo from './index';

export = (ctx: Hexo): Promise<void[][]> => {
export default function loadPlugins(ctx: Hexo): Promise<void[][]> {
Copy link
Preview

Copilot AI Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing from CommonJS export = to ES modules export default is a breaking change that affects all existing imports. Consider keeping the original export pattern or creating a separate utility module for testable functions.

Copilot uses AI. Check for mistakes.

import chai from 'chai';
import loadPlugins, { displayPath } from '../../../lib/hexo/load_plugins';

const should = chai.should();
Copy link
Preview

Copilot AI Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The should variable is declared but never used. The tests use result.should.equal() which works through chai's extension of Object.prototype.

Suggested change
const should = chai.should();
// Removed unused `should` variable declaration.

Copilot uses AI. Check for mistakes.

@stevenjoezhang
Copy link
Member

You can place the unit test in hexo/test/scripts/hexo/load_plugins.ts and fix any regressions it causes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants