Skip to content
This repository has been archived by the owner on Aug 5, 2020. It is now read-only.

Ignore file dependencies when checking mismatches. #1185

Merged
merged 2 commits into from
May 1, 2018
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* @flow */
jest.mock('../getSingleEntryFromGenerator.js', () => jest.fn());
jest.mock('gluestick-generators', () => ({
parseConfig: jest.fn(() => ({
parseConfig: () => ({
entry: {
template: JSON.stringify({
dependencies: {
Expand All @@ -13,37 +13,41 @@ jest.mock('gluestick-generators', () => ({
},
}),
},
})),
}),
}));

const utils = require('../utils');
const checkForMismatch = require('../checkForMismatch');

const orignialPromptModulesUpdate = utils.promptModulesUpdate;
const originalPromptModulesUpdate = utils.promptModulesUpdate;

describe('autoUpgrade/checkForMismatch', () => {
let checkForMismatch;
beforeEach(() => {
utils.promptModulesUpdate = jest.fn(() =>
Promise.resolve({ shouldFix: true, mismatchedModules: {} }),
Promise.resolve({ shouldFix: false, mismatchedModules: {} }),
);
checkForMismatch = require('../checkForMismatch');
});

afterEach(() => {
jest.resetAllMocks();
utils.promptModulesUpdate = orignialPromptModulesUpdate;
jest.resetModules();
utils.promptModulesUpdate = originalPromptModulesUpdate;
});

it('should detect mismatched modules', done => {
// $FlowIgnore
checkForMismatch({
dependencies: {
depA: '1.0.0',
depB: '1.0.0',
it('detects mismatched modules', () => {
return checkForMismatch(
{
dependencies: {
depA: '1.0.0',
depB: '1.0.0',
},
devDependencies: {},
},
}).then(result => {
false,
).then(result => {
expect(result).toBeTruthy();
// $FlowIgnore
expect(utils.promptModulesUpdate.mock.calls[0][0]).toEqual({
expect(utils.promptModulesUpdate).toHaveBeenCalledWith({
depA: {
required: '2.0.0',
project: '1.0.0',
Expand All @@ -55,7 +59,24 @@ describe('autoUpgrade/checkForMismatch', () => {
type: 'devDependencies',
},
});
done();
});
});

it('ignores local file dependencies', () => {
return checkForMismatch(
{
dependencies: {
depA: 'file:../gluestick/packages/gluestick',
depB: '1.0.0',
},
devDependencies: {
depC: '1.0.0',
},
},
false,
).then(result => {
expect(result).toBeTruthy();
expect(utils.promptModulesUpdate).not.toHaveBeenCalled();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@ jest.mock('fs', () => ({
jest.mock('inquirer', () => ({
prompt: () => Promise.resolve({ confirm: true }),
}));
const {
isValidVersion,
replaceFile,
promptModulesUpdate,
} = require('../utils');
const fs = require('fs');
const path = require('path');
const { isValidVersion, promptModulesUpdate } = require('../utils');

describe('autoUpgrade/utils.isValidVersion', () => {
it('should return true when version is greater than or equal requiredVersion', () => {
Expand Down Expand Up @@ -52,23 +46,6 @@ describe('autoUpgrade/utils.isValidVersion', () => {
});
});

describe('autoUpgrade/utils.replaceFile', () => {
it('should replace file', () => {
// $FlowIgnore only info from logger is used
replaceFile(
{
info: jest.fn(),
},
'test',
'data',
);
expect(fs.writeFileSync.mock.calls[0]).toEqual([
path.join(process.cwd(), 'test'),
'data',
]);
});
});

describe('autoUpgrade/utils.promptModulesUpdate', () => {
it('should replace file', done => {
const mismatchedModules = {
Expand Down
41 changes: 20 additions & 21 deletions packages/gluestick/src/commands/autoUpgrade/checkForMismatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,25 @@ import type { MismatchedModules, UpdateDepsPromptResults } from '../../types';
const path = require('path');
const getSingleEntryFromGenerator = require('./getSingleEntryFromGenerator');
const parseConfig = require('gluestick-generators').parseConfig;
const utils = require('./utils');
const { isValidVersion, promptModulesUpdate } = require('./utils');
const version = require('../../../package.json').version;

type ProjectPackage = {
dependencies: Object,
devDependencies: Object,
};

const isFileDependency = (name: string) => {
return name.startsWith('file');
};

const isMismatched = (project, template) => {
return (
!project ||
(!isValidVersion(project, template) && !isFileDependency(project))
);
};

/**
* Open the package.json file in both the project as well as the one used by
* this command line interface, then compare the versions for shared modules.
Expand Down Expand Up @@ -40,7 +51,6 @@ const checkForMismatch = (
dev: boolean,
): Promise<UpdateDepsPromptResults> => {
// This is done to keep live reference to mock single function in testing
const { isValidVersion, promptModulesUpdate } = utils;
const projectPackage: ProjectPackage = {
dependencies: {},
devDependencies: {},
Expand Down Expand Up @@ -74,31 +84,20 @@ const checkForMismatch = (
};
};
Object.keys(templatePackage.dependencies).forEach((dep: string): void => {
if (
dev &&
dep === 'gluestick' &&
!/\d+\.\d+\.\d+.*/.test(projectPackage.dependencies[dep])
) {
const project = projectPackage.dependencies[dep];
const template = templatePackage.dependencies[dep];
if (dev && dep === 'gluestick' && !/\d+\.\d+\.\d+.*/.test(project)) {
return;
}
if (
!projectPackage.dependencies[dep] ||
!isValidVersion(
projectPackage.dependencies[dep],
templatePackage.dependencies[dep],
)
) {
if (isMismatched(project, template)) {
markMissing(dep, 'dependencies');
}
});
Object.keys(templatePackage.devDependencies).forEach((dep: string): void => {
if (
!projectPackage.devDependencies[dep] ||
!isValidVersion(
projectPackage.devDependencies[dep],
templatePackage.devDependencies[dep],
)
) {
const project = projectPackage.devDependencies[dep];
const template = templatePackage.devDependencies[dep];

if (isMismatched(project, template)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This reads a lot nicer 👍

markMissing(dep, 'devDependencies');
}
});
Expand Down
18 changes: 0 additions & 18 deletions packages/gluestick/src/commands/autoUpgrade/utils.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,14 @@
/* @flow */
import type {
Logger,
Question,
MismatchedModules,
UpdateDepsPromptResults,
} from '../../types';

const path = require('path');
const fs = require('fs');
const semver = require('semver');
const chalk = require('chalk');
const inquirer = require('inquirer');

/**
* Let the user know that we are updating the file and copy the contents over.
*
* @param {String} name the name of the file
* @param {String} data the data for the new file
*/
const replaceFile = (logger: Logger, name: string, data: string): void => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like replaceFile just wasn't used anywhere hm?

const filePath: string = path.join(process.cwd(), name);

logger.info(`${name} file is out of date.`);
logger.info(`Updating ${filePath}`);
fs.writeFileSync(filePath, data);
};

/**
* Determine a version meets or exceeds a requirement.
*
Expand Down Expand Up @@ -90,6 +73,5 @@ const promptModulesUpdate = (

module.exports = {
isValidVersion,
replaceFile,
promptModulesUpdate,
};