Skip to content
Snippets Groups Projects
Commit 94695548 authored by Rhys Arkins's avatar Rhys Arkins
Browse files

refactor(cocoapods): use autoReplace

parent 0ab9de42
Branches
Tags
No related merge requests found
import * as rubyVersioning from '../../versioning/ruby';
export { extractPackageFile } from './extract';
export { updateDependency } from './update';
export { updateArtifacts } from './artifacts';
export const defaultConfig = {
......
import fs from 'fs-extra';
import path from 'path';
import { updateDependency } from '.';
const fileContent = fs.readFileSync(
path.resolve(__dirname, './__fixtures__/Podfile.simple'),
'utf-8'
);
describe('lib/manager/cocoapods/update', () => {
describe('updateDependency', () => {
it('handles undefined lines', () => {
const upgrade = {
depName: 'b',
managerData: { lineNumber: 999999999 },
currentValue: '1.2.3',
newValue: '2.0.0',
};
const res = updateDependency({ fileContent, upgrade });
expect(res).toBeNull();
});
it('replaces existing value', () => {
const upgrade = {
depName: 'b',
managerData: { lineNumber: 4 },
currentValue: '1.2.3',
newValue: '2.0.0',
};
const res = updateDependency({ fileContent, upgrade });
expect(res).not.toEqual(fileContent);
expect(res.includes(upgrade.newValue)).toBe(true);
});
it('returns same content', () => {
const upgrade = {
depName: 'b',
managerData: { lineNumber: 4 },
currentValue: '1.2.3',
newValue: '1.2.3',
};
const res = updateDependency({ fileContent, upgrade });
expect(res).toEqual(fileContent);
expect(res).toBe(fileContent);
});
it('returns null', () => {
const upgrade = {
depName: 'b',
managerData: { lineNumber: 0 },
currentValue: '1.2.3',
newValue: '2.0.0',
};
const res = updateDependency({ fileContent, upgrade });
expect(res).toBeNull();
});
});
});
import { logger } from '../../logger';
import { UpdateDependencyConfig } from '../common';
import { parseLine } from './extract';
function lineContainsDep(line: string, dep: string): boolean {
const { depName } = parseLine(line);
return dep === depName;
}
export function updateDependency({
fileContent,
upgrade,
}: UpdateDependencyConfig): string | null {
const { currentValue, managerData, depName, newValue } = upgrade;
// istanbul ignore if
if (!currentValue || !managerData || !depName) {
logger.warn('Cocoapods: invalid upgrade object');
return null;
}
logger.debug(`cocoapods.updateDependency: ${newValue}`);
const lines = fileContent.split('\n');
const lineToChange = lines[managerData.lineNumber];
if (!lineToChange) {
logger.warn(
{ fileContent, depName, currentValue, newValue },
'Undefined cocoapods lineToChange'
);
return null;
}
if (!lineContainsDep(lineToChange, depName)) {
return null;
}
const regex = new RegExp(`(['"])${currentValue.replace('.', '\\.')}\\1`);
const newLine = lineToChange.replace(regex, `$1${newValue}$1`);
if (newLine === lineToChange) {
logger.debug('No changes necessary');
return fileContent;
}
lines[managerData.lineNumber] = newLine;
return lines.join('\n');
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment