Skip to content

Commit

Permalink
update go import regex to consider pre v2 (#174)
Browse files Browse the repository at this point in the history
* update go import regex to consider pre v2

* add missing file
  • Loading branch information
meorphis authored Sep 27, 2024
1 parent 919b93a commit bfe4bee
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/updaters/go/github-imports-go.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import {DefaultUpdater} from '../default';

export class GithubImportsGo extends DefaultUpdater {
updateContent(content: string): string {
if (this.version.major < 2) {
return content;
}

return content.replace(
/"github\.com\/([^/]+)\/([^/]+)\/v([1-9]\d*)\/(.+)"/g,
(_, user, repo, __, path) =>
/"github\.com\/([^/]+)\/([^/]+)(\/v([1-9]\d*))?\/(.+)"/g,
(_, user, repo, __, ___, path) =>
`"github.com/${user}/${repo}/v${this.version.major.toString()}/${path}"`
);
}
Expand Down
18 changes: 18 additions & 0 deletions test/updaters/fixtures/file-with-imports-v1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package accounts

import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"time"

"github.com/cloudflare/cloudflare-go/internal/apijson"
"github.com/cloudflare/cloudflare-go/internal/apiquery"
"github.com/cloudflare/cloudflare-go/internal/pagination"
"github.com/cloudflare/cloudflare-go/internal/param"
"github.com/cloudflare/cloudflare-go/internal/requestconfig"
"github.com/cloudflare/cloudflare-go/option"
"github.com/cloudflare/cloudflare-go/shared"
)
23 changes: 21 additions & 2 deletions test/updaters/go-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import {GithubImportsGo} from '../../src/updaters/go/github-imports-go';
const fixturesPath = './test/updaters/fixtures';

describe('GithubImportsGo', () => {
const v1File = readFileSync(
resolve(fixturesPath, 'file-with-imports-v1.go'),
'utf8'
);

const v2File = readFileSync(
resolve(fixturesPath, 'file-with-imports-v2.go'),
'utf8'
Expand All @@ -18,14 +23,28 @@ describe('GithubImportsGo', () => {
'utf8'
);

it('makes no changes if the new version has a major version of 2', async () => {
it('makes no changes if the old version has a major version of 1 and the new version also has a major version of 1', async () => {
const readmeUpdater = new GithubImportsGo({
version: Version.parse('1.0.0'),
});
expect(readmeUpdater.updateContent(v1File)).to.equal(v1File);
});

it('updates the version in the imports if the old version has a major version of 1 and the new version has a major version of 2', async () => {
const readmeUpdater = new GithubImportsGo({
version: Version.parse('2.0.0'),
});
expect(readmeUpdater.updateContent(v1File)).to.equal(v2File);
});

it('makes no changes if the old version has a major version of 2 and the new version also has a major version of 2', async () => {
const readmeUpdater = new GithubImportsGo({
version: Version.parse('2.0.0'),
});
expect(readmeUpdater.updateContent(v2File)).to.equal(v2File);
});

it('updates the version in the imports if the new version has a major version of 3', async () => {
it('updates the version in the imports if the old version has a major version of 2 and the new version has a major version of 3', async () => {
const readmeUpdater = new GithubImportsGo({
version: Version.parse('3.0.0'),
});
Expand Down

0 comments on commit bfe4bee

Please sign in to comment.