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

fix: normalize paths for Gradle on Windows #130

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"semver": "^5.0.3",
"serve-static": "^1.13.1",
"shell-quote": "1.6.1",
"slash": "^2.0.0",
"ws": "^1.1.0",
"xcode": "^1.0.0",
"xmldoc": "^0.4.0"
Expand Down
118 changes: 69 additions & 49 deletions packages/cli/src/link/__tests__/android/makeSettingsPatch-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,78 +4,98 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @emails oncall+javascript_foundation
* @flow
*/

const path = require('path');
const makeSettingsPatch = require('../../android/patches/makeSettingsPatch');
const normalizeProjectName = require('../../android/patches/normalizeProjectName');

const name = 'test';
const scopedName = '@scoped/test';
const normalizedScopedName = normalizeProjectName('@scoped/test');
const projectConfig = {
sourceDir: '/home/project/android/app',
settingsGradlePath: '/home/project/android/settings.gradle',
};
const dependencyConfig = {
sourceDir: `/home/project/node_modules/${name}/android`,
};
const scopedDependencyConfig = {
sourceDir: `/home/project/node_modules/${scopedName}/android`,
};

describe('makeSettingsPatch', () => {
describe('makeSettingsPatch with package "test"', () => {
const name = 'test';
const dependencyConfig = {
sourceDir: `/home/project/node_modules/${name}/android`,
};

it('should build a patch function', () => {
expect(
Object.prototype.toString(
makeSettingsPatch(name, dependencyConfig, projectConfig)
)
).toBe('[object Object]');
makeSettingsPatch(name, dependencyConfig, projectConfig)
).toMatchObject({
pattern: '\n',
patch: expect.any(String),
});
});

it('should make a correct patch', () => {
const projectDir = path.relative(
path.dirname(projectConfig.settingsGradlePath),
dependencyConfig.sourceDir
Copy link
Member Author

Choose a reason for hiding this comment

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

gotta love these tests that recreate implementation 1-to-1 :D

);

it('includes project with correct path', () => {
const { patch } = makeSettingsPatch(name, dependencyConfig, projectConfig);

expect(patch).toBe(
`include ':${name}'\n` +
`project(':${name}').projectDir = ` +
`new File(rootProject.projectDir, '${projectDir}')\n`
expect(patch).toMatchInlineSnapshot(`
"include ':test'
project(':test').projectDir = new File(rootProject.projectDir, '../node_modules/test/android')
"
`);
});

// Simulate Windows environment on POSIX filesystem
// TODO: scope this test to Windows-only once we setup CI on Windows
it('includes project with correct path on Windows', () => {
jest.resetModules();
jest.doMock('path', () => {
const path = jest.requireActual('path');
path.dirname = path.win32.dirname;
path.relative = path.win32.relative;
return path;
});
// eslint-disable-next-line no-shadow
const makeSettingsPatch = require('../../android/patches/makeSettingsPatch');
const projectConfigWindows = {
sourceDir: 'C:\\home\\project\\android\\app',
settingsGradlePath: 'C:\\home\\project\\android\\settings.gradle',
};
const dependencyConfigWindows = {
sourceDir: `C:\\home\\project\\node_modules\\${name}\\android`,
};
const { patch } = makeSettingsPatch(
name,
dependencyConfigWindows,
projectConfigWindows
);

jest.dontMock('path');

expect(patch).toMatchInlineSnapshot(`
"include ':test'
project(':test').projectDir = new File(rootProject.projectDir, '../node_modules/test/android')
"
`);
});
});

describe('makeSettingsPatchWithScopedPackage', () => {
describe('makeSettingsPatch with scoped package "@scoped/test"', () => {
const name = '@scoped/test';
const dependencyConfig = {
sourceDir: `/home/project/node_modules/${name}/android`,
};

it('should build a patch function', () => {
expect(
Object.prototype.toString(
makeSettingsPatch(scopedName, scopedDependencyConfig, projectConfig)
)
).toBe('[object Object]');
makeSettingsPatch(name, dependencyConfig, projectConfig)
).toMatchObject({
pattern: '\n',
patch: expect.any(String),
});
});

it('should make a correct patch', () => {
const projectDir = path.relative(
path.dirname(projectConfig.settingsGradlePath),
scopedDependencyConfig.sourceDir
);

const { patch } = makeSettingsPatch(
scopedName,
scopedDependencyConfig,
projectConfig
);
it('includes project with correct path', () => {
const { patch } = makeSettingsPatch(name, dependencyConfig, projectConfig);

expect(patch).toBe(
`include ':${normalizedScopedName}'\n` +
`project(':${normalizedScopedName}').projectDir = ` +
`new File(rootProject.projectDir, '${projectDir}')\n`
);
expect(patch).toMatchInlineSnapshot(`
"include ':@scoped_test'
project(':@scoped_test').projectDir = new File(rootProject.projectDir, '../node_modules/@scoped/test/android')
"
`);
});
});
10 changes: 7 additions & 3 deletions packages/cli/src/link/android/patches/makeSettingsPatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@
*/

const path = require('path');
const slash = require('slash');
const normalizeProjectName = require('./normalizeProjectName');

module.exports = function makeSettingsPatch(
name,
androidConfig,
projectConfig
) {
const projectDir = path.relative(
path.dirname(projectConfig.settingsGradlePath),
androidConfig.sourceDir
// Gradle expects paths to be posix even on Windows
const projectDir = slash(
path.relative(
path.dirname(projectConfig.settingsGradlePath),
androidConfig.sourceDir
)
);
const normalizedProjectName = normalizeProjectName(name);

Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8192,6 +8192,7 @@ slash@^1.0.0:
slash@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/slash/-/slash-2.0.0.tgz#de552851a1759df3a8f206535442f5ec4ddeab44"
integrity sha512-ZYKh3Wh2z1PpEXWr0MpSBZ0V6mZHAQfYevttO11c51CaWjGTaadiKZ+wVt1PbMlDV5qhMFslpZCemhwOK7C89A==

slice-ansi@1.0.0:
version "1.0.0"
Expand Down