Skip to content

Commit

Permalink
fix(maven): Rename module.json to dist.module (#540)
Browse files Browse the repository at this point in the history
  • Loading branch information
markushi authored May 23, 2024
1 parent d803f3a commit de86d16
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
108 changes: 108 additions & 0 deletions src/targets/__tests__/mavenDiskIo.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { NoneArtifactProvider } from '../../artifact_providers/none';
import {
MavenTarget
} from '../maven';
import { withTempDir } from '../../utils/files';
import { promises as fsPromises } from 'fs';
import { join } from 'path';

jest.mock('../../utils/gpg');
jest.mock('../../utils/system');

const DEFAULT_OPTION_VALUE = 'my_default_value';

function getRequiredTargetConfig(): any {
return {
GPG_PASSPHRASE: DEFAULT_OPTION_VALUE,
OSSRH_USERNAME: DEFAULT_OPTION_VALUE,
OSSRH_PASSWORD: DEFAULT_OPTION_VALUE,
mavenCliPath: DEFAULT_OPTION_VALUE,
mavenSettingsPath: DEFAULT_OPTION_VALUE,
mavenRepoId: DEFAULT_OPTION_VALUE,
mavenRepoUrl: DEFAULT_OPTION_VALUE,
android: false,
kmp: false,
};
}

function createMavenTarget(
targetConfig?: Record<string, unknown>
): MavenTarget {
process.env.GPG_PRIVATE_KEY = DEFAULT_OPTION_VALUE;
process.env.GPG_PASSPHRASE = DEFAULT_OPTION_VALUE;
process.env.OSSRH_USERNAME = DEFAULT_OPTION_VALUE;
process.env.OSSRH_PASSWORD = DEFAULT_OPTION_VALUE;

const finalConfig = targetConfig ? targetConfig : getRequiredTargetConfig();
const mergedConfig = {
name: 'maven',
...finalConfig,
};
return new MavenTarget(mergedConfig, new NoneArtifactProvider());
}

describe('maven disk io', () => {
test('fileExists', async () => {
await withTempDir(async (tmpDir): Promise<void> => {
const target = createMavenTarget();

expect(await target.fileExists('a/random/path')).toBe(false);

// a folder should return false
expect(await target.fileExists(tmpDir)).toBe(false);

const file = join(tmpDir, 'module.json');

// when the file doesn't exist it should return false
expect(await target.fileExists(file)).toBe(false);
await fsPromises.writeFile(file, 'abc');

// once the file is written, it should exist
expect(await target.fileExists(file)).toBe(true);

});
});

test('fixModuleFileName', async () => {
await withTempDir(async (tmpDir): Promise<void> => {
const target = createMavenTarget();

const file = join(tmpDir, 'module.json');
await fsPromises.writeFile(file, 'abc');

const moduleFile = join(tmpDir, 'sentry-java-1.0.0.module');
// when fix module is called with proper file names
await target.fixModuleFileName(tmpDir, moduleFile);

// it should rename the file
expect(await target.fileExists(file)).toBe(false);
expect(await target.fileExists(moduleFile)).toBe(true);

});
});

test('fixModuleFileName no-op', async () => {
await withTempDir(async (tmpDir): Promise<void> => {
const target = createMavenTarget();

const file = join(tmpDir, 'sentry-java-1.0.0.module');
await fsPromises.writeFile(file, 'abc');

// when fix module is called, but the proper file already exists
await target.fixModuleFileName(tmpDir, file);

// it should still exist after calling fixModuleFileName
expect(await target.fileExists(file)).toBe(true);

});
});

test('fixModuleFileName non-existant-files', async () => {
await withTempDir(async (tmpDir): Promise<void> => {
const target = createMavenTarget();

const file = join(tmpDir, 'sentry-java-1.0.0.module');
await target.fixModuleFileName(tmpDir, file);
});
});
});
33 changes: 33 additions & 0 deletions src/targets/maven.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,36 @@ export class MavenTarget extends BaseTarget {
return undefined;
}

/**
* Returns true if the provided path exists and is a file.
*/
public async fileExists(filePath: string): Promise<boolean> {
try {
const stat = await fsPromises.stat(filePath);
if (stat.isFile()) {
return true;
}
} catch (e) {
// ignored
}
return false;
}

/**
* Some maven publications contain a dist/module.json file instead of a
* mvn friendly dist/dist.module file.
*
* In case module.json exists but dist.module doesn't,
* this function renames module.json to dist.module,
* making it fit for mvn publishing.
*/
public async fixModuleFileName(distDir: string, moduleFile: string): Promise<void> {
const fallbackFile = join(distDir, 'module.json');
if (!await this.fileExists(moduleFile) && await this.fileExists(fallbackFile)) {
await fsPromises.rename(fallbackFile, moduleFile);
}
}

private async uploadPomDistribution(distDir: string): Promise<void> {
if (this.mavenConfig.kmp !== false) {
await this.uploadKmpPomDistribution(distDir);
Expand All @@ -508,6 +538,7 @@ export class MavenTarget extends BaseTarget {
pomFile,
moduleFile,
} = this.getFilesForMavenPomDist(distDir);
await this.fixModuleFileName(distDir, moduleFile);

// Maven central is very flaky, so retrying with an exponential delay in
// in case it fails.
Expand Down Expand Up @@ -559,6 +590,8 @@ export class MavenTarget extends BaseTarget {
string,
string | string[]
>;
await this.fixModuleFileName(distDir, files.moduleFile as string);

const moduleName = parse(distDir).base;
if (this.mavenConfig.kmp !== false) {
const isRootDistDir = this.mavenConfig.kmp.rootDistDirRegex.test(
Expand Down

0 comments on commit de86d16

Please sign in to comment.