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

Add checkDirExists endpoint + tests #38

Merged
merged 4 commits into from
Oct 13, 2023

Conversation

lansolo99
Copy link
Contributor

@lansolo99 lansolo99 commented Oct 11, 2023

This PR closes #13
I mostly created derived logic from checkIfFileExists.
I also created the relative tests. I was able to find how to test the status 400 properly, it fallbacks to 404.

I noticed a lot of test errors when executing bun test
My coverage is 92%.

@Xkonti
Copy link
Owner

Xkonti commented Oct 11, 2023

Tests are suggesting issue with the positive test:

1 tests failed:
(fail) should return HTTP Status 204 if the directory is found [0.95ms]
Check failure on line 37 in src/endpoints/dir/checkDirExists.test.ts

GitHub Actions
/ check-pr
error: expect(received).toBe(expected)
Expected: 204
Received: 404

@lansolo99
Copy link
Contributor Author

@Xkonti Fixed the status 204 test by adding proper filter to the tested directories terms.

Copy link
Owner

@Xkonti Xkonti left a comment

Choose a reason for hiding this comment

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

Looks great. There's just one last thing before I can merge it in. Thank you!

Comment on lines 11 to 19
let relativePath = query.path ? (query.path as string) : null;
if (!isPathValid(relativePath)) {
set.status = 400;
return provideValidPathDirMsg;
}
relativePath = relativePath as string;

// Get the full path to the directory
const dirPath = join(getConfig().dataDir, relativePath);
Copy link
Owner

Choose a reason for hiding this comment

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

You can get the relative path and absolute path at the same time using validateRelativePath. This way we will avoid same checking logic in multiple spots:

// Performs validation of the `query.path`
const dirPathValidationResult = validateRelativePath(query.path);
if (dirPathValidationResult.isErr()) {
  set.status = 400;
  return provideValidPathDirMsg;
}
// Create variable to hold relative and absolute path
const dirPath = sourcePathValidationResult.value;

// Using the relative and absolute path:
let something = dirPath.relativePath;
let somethingElse = dirPath.absolutePath;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I reused it. Though the absolutePath doesn't seems necessary for this endpoint, I just used the relativePath from the value.

Copy link
Owner

Choose a reason for hiding this comment

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

Looks like you're using the absolute path in line 19:

// Get the full path to the directory
const dirPath = join(getConfig().dataDir, relativePath);

Here's whot it looks like in validateRealativePath:

// Get the full path to the file
const relativePath = path as string;
const absolutePath = join(getConfig().dataDir, relativePath);
return ok({relativePath, absolutePath});

@lansolo99
Copy link
Contributor Author

@Xkonti I didn't notice it was the same piece of logic, my bad. So I only use the already returned absolutePath this time.

Copy link
Owner

@Xkonti Xkonti left a comment

Choose a reason for hiding this comment

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

Looks awesome. Thanks!

@Xkonti Xkonti merged commit 5c8ffc8 into Xkonti:main Oct 13, 2023
1 check passed
@lansolo99 lansolo99 deleted the checkIfDirectoryExists branch October 17, 2023 12:55
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.

Check if directory exists
2 participants