Skip to content

Conversation

@P0lip
Copy link
Contributor

@P0lip P0lip commented Jun 20, 2019

Question:

TODO:

  • relative helper

@P0lip P0lip requested review from billiegoose and marbemac June 20, 2019 03:35
@marbemac
Copy link

Should we add tests for actual URIs?

Yeah I think we should.

should toFSPath return a Windows-compatible path when executed under Windows?

What do you mean?

@marbemac
Copy link

Could you try yalcing this into graphite, json-ref-resolver, and studio if you have time today? And updating various usages in those projects to make sure these utils feel good? Thank you!

@P0lip
Copy link
Contributor Author

P0lip commented Jun 20, 2019

should toFSPath return a Windows-compatible path when executed under Windows?

What I'm saying is whether the fn should generate the following path c:\foo\foo under Windows.

@P0lip
Copy link
Contributor Author

P0lip commented Jun 20, 2019

@billiegoose
Copy link
Contributor

What I'm saying is whether the fn should generate the following path c:\foo\foo under Windows.

No. Windows is perfectly happy with forward slash separators. The only part of Windows that doesn't like forward slash separators are old DOS shell commands that use forward slash to indicate CLI arguments (instead of dash like POSIX CLIs).

The only reason to use back slashes would be if users complain. There is no technical reason why we should convert to back slashes on certain platforms.

@marbemac
Copy link

What I'm saying is whether the fn should generate the following path c:\foo\foo under Windows.

Andddd a primary purpose of this path package is to normalize windows and posix where we can (in this case, to always use forward slashes). So that graphite lookup by uri works across os, etc.

@marbemac
Copy link

marbemac commented Jun 23, 2019

I'm still not sure why we don't just use path where possible (normalize, join, dirname, basename)? Maybe the codebase inside of path is not perfect, but it works... the only thing we need to do when using path is make sure that the result is using posix style slashes.

Besides that, I think we still need to cover a few things:

  1. What happens when urls are passed into these functions? They don't all have to support urls, but we should at least document and know what happens. For example, does isAbsolute return true if a url is passed in right now? (we need it to).
  2. Suggest adding relative.

In general, I'd suggest that for almost every method (perhaps not toFSPath) we simply:

  1. check if is URL (for join, check if first part is url)
  2. IF url, use appropriate urijs method
  3. IF not url, use path

Basically, we need to:

  1. support urls, posix style file paths, and windows style file paths.
  2. normalize file paths to forward slashes

I think that will cover all of our use cases?

@billiegoose
Copy link
Contributor

billiegoose commented Jun 24, 2019

the only thing we need to do when using path is make sure that the result is using posix style slashes.

Unfortunately @marbemac, that's simply not true.

At a minimum we should stop using path and change our dependency to use path-browserify v1.0.0 so that we aren't depending on an unknown codebase that changes at the whim of bundlers and Electron versions and behaves completely differently depending on the environment.

In an ideal we'd use our own parser so we have full control and can understand what the library is doing, as I suggest in #2.

Edit: The reason that the minimum is OK is because at least then we have predictable behavior. I.e. the same code paths will be running whether the environment is Webpack, Jest, Node on Windows, or Node on Mac. Developers will be able to do things like, import { sep } from '@stoplight/path' and not be introducing bugs that only affect certain platforms.

@billiegoose
Copy link
Contributor

In fact I feel so strongly about the "minimum" I'm going to change that right now and push it to this PR hold on.

@P0lip
Copy link
Contributor Author

P0lip commented Jun 24, 2019

@wmhilton hold on with pushing to this branch. I'm about to push a commit.

@billiegoose
Copy link
Contributor

ok. It shouldn't have any merge conflicts though, just swapping out from 'path' for from 'path-browserify'

@billiegoose
Copy link
Contributor

I'm gonna re-run tests on Windows, make sure nothing is amiss

@billiegoose
Copy link
Contributor

Tests pass on Windows for what that's worth:
image

Copy link
Contributor

@billiegoose billiegoose left a comment

Choose a reason for hiding this comment

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

Left a couple suggestions

});

it('treats URLs as file URIs', () => {
expect(join('https://foo.test', 'com', 'baz')).toEqual('/com/baz');
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
expect(join('https://foo.test', 'com', 'baz')).toEqual('/com/baz');
expect(join('https://foo.test', 'com', 'baz')).toEqual('https://foo.test/com/baz');

Choose a reason for hiding this comment

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

treats URLs as file URIs

Yeah in general I think the premise of the test is wrong. URLs should be treated as URLs. File paths as file paths.

Should be able to pass the following in (prob not comprehensive, but ya get the idea):

  • join('https://foo.test', 'com', 'baz')
  • join('https://foo.com/pets', '..', 'users', 123)
  • join('/foo/bar', '..', 'baz')
  • join('c:/foo/bar', '..', 'baz')
  • join('c:/foo\bar', '..', 'baz')

Copy link

@marbemac marbemac left a comment

Choose a reason for hiding this comment

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

Closer!

import { relative } from '../';

describe('relative', () => {
it('handles URLs', () => {

Choose a reason for hiding this comment

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

Should also add some tests to make sure it handles the various file path cases (posix, windows, forward slash, mixed slash, etc).

});

it('treats URLs as file URIs', () => {
expect(join('https://foo.test', 'com', 'baz')).toEqual('/com/baz');

Choose a reason for hiding this comment

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

treats URLs as file URIs

Yeah in general I think the premise of the test is wrong. URLs should be treated as URLs. File paths as file paths.

Should be able to pass the following in (prob not comprehensive, but ya get the idea):

  • join('https://foo.test', 'com', 'baz')
  • join('https://foo.com/pets', '..', 'users', 123)
  • join('/foo/bar', '..', 'baz')
  • join('c:/foo/bar', '..', 'baz')
  • join('c:/foo\bar', '..', 'baz')

});

it('handles Windows URIs', () => {
expect(relative('c:\\test\\baz', 'C:\\test\\foo')).toEqual('../../../C:/test/foo');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@marbemac weird things happening here.

@marbemac marbemac requested review from marbemac and removed request for marbemac June 28, 2019 22:18
@marbemac marbemac dismissed their stale review June 28, 2019 22:19

looking much better - will's going to finish up the review!

@billiegoose
Copy link
Contributor

@P0lip can I humbly suggest closing this PR in favor of #2 ?

@billiegoose billiegoose mentioned this pull request Jun 30, 2019
@P0lip P0lip closed this Jun 30, 2019
@P0lip
Copy link
Contributor Author

P0lip commented Jun 30, 2019

Closing favor of #2

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.

4 participants