-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ken Howard
committed
Nov 29, 2018
1 parent
9ef1fba
commit 7c9518e
Showing
10 changed files
with
190 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// tslint:disable:no-magic-numbers no-any | ||
|
||
import { GISTS_BASE_URL } from '../../constants'; | ||
|
||
import { up } from '../migrate-2.0.0'; | ||
|
||
const state = { | ||
get: jest.fn(), | ||
update: jest.fn() | ||
}; | ||
|
||
describe('Migrate to 2.0.0 Tests', () => { | ||
test('should retrieve gisttoken and create a new profile', () => { | ||
expect.assertions(3); | ||
|
||
state.get.mockReturnValueOnce('myaccesstoken'); | ||
|
||
up(state, () => { | ||
// return void | ||
}); | ||
|
||
expect(state.update.mock.calls[0]).toStrictEqual(['gisttoken', undefined]); | ||
expect(state.update.mock.calls[1]).toStrictEqual([ | ||
'gist_provider', | ||
undefined | ||
]); | ||
expect(state.update.mock.calls[2]).toStrictEqual([ | ||
'profiles', | ||
{ github: { active: true, key: 'myaccesstoken', url: GISTS_BASE_URL } } | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// tslint:disable:no-magic-numbers no-any | ||
|
||
import { migrations } from '../migration-service'; | ||
|
||
const state = { | ||
get: jest.fn(() => ''), | ||
update: jest.fn() | ||
}; | ||
|
||
describe('Migrations Tests', () => { | ||
test('should process migrations', () => { | ||
expect.assertions(3); | ||
|
||
const migrationCallback = jest.fn((_s, c) => { | ||
c(); | ||
}); | ||
|
||
const migrationOne: any = ['mymigrationone', migrationCallback]; | ||
const migrationTwo: any = ['mygrationtwo', migrationCallback]; | ||
const finalCallback = jest.fn(); | ||
|
||
migrations.configure({ migrations: [migrationOne, migrationTwo], state }); | ||
migrations.up(finalCallback); | ||
|
||
expect(finalCallback).toHaveBeenCalled(); | ||
expect(state.update.mock.calls).toHaveLength(2); | ||
expect(migrationCallback.mock.calls).toHaveLength(2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './migration-service'; | ||
export * from './migrations'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Memento } from 'vscode'; | ||
|
||
import { GISTS_BASE_URL } from '../constants'; | ||
|
||
export const up = ( | ||
state: Memento, | ||
cb: (err: Error | undefined) => void | ||
): void => { | ||
let error: Error | undefined; | ||
try { | ||
const key = state.get<string>('gisttoken'); | ||
state.update('gisttoken', undefined); | ||
state.update('gist_provider', undefined); | ||
if (key) { | ||
const url = GISTS_BASE_URL; | ||
const name = 'github'; | ||
const active = true; | ||
|
||
state.update('profiles', { [name]: { active, key, url } }); | ||
} | ||
} catch (err) { | ||
error = err; | ||
} | ||
|
||
cb(error); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { Memento, workspace } from 'vscode'; | ||
|
||
import { logger } from '../logger'; | ||
|
||
type Migration = [ | ||
string, | ||
(state: Memento, callback: (error?: Error) => void) => void | ||
]; | ||
|
||
class MigrationService { | ||
public static getInstance = (): MigrationService => | ||
(MigrationService.instance = MigrationService.instance | ||
? MigrationService.instance | ||
: new MigrationService()) | ||
|
||
private static instance?: MigrationService; | ||
|
||
private migrations: Migration[] = []; | ||
private state: Memento = workspace.getConfiguration(); | ||
|
||
private constructor() {} | ||
|
||
public configure(options: { migrations: Migration[]; state: Memento }): void { | ||
this.state = options.state; | ||
this.migrations = options.migrations; | ||
} | ||
|
||
public up( | ||
cb: (err: Error | undefined, results: { migrated: string[] }) => void | ||
): void { | ||
const migrated: string[] = []; | ||
let error: Error | undefined; | ||
|
||
try { | ||
this.migrations.forEach((m) => { | ||
const [migrationName, migrationFn] = m; | ||
if (this.do(migrationName)) { | ||
logger.debug(`migrating ${migrationName}`); | ||
migrationFn(this.state, (err) => { | ||
if (err) { | ||
logger.error(`could not migrate to ${migrationName}`); | ||
throw err; | ||
} | ||
this.recordMigration(migrationName); | ||
migrated.push(migrationName); | ||
}); | ||
} | ||
}); | ||
} catch (err) { | ||
error = err; | ||
} | ||
|
||
cb(error, { migrated }); | ||
} | ||
|
||
private do(migrationName: string): boolean { | ||
const pastMigrations = this.state.get('migrations', '').split(';'); | ||
|
||
return pastMigrations.indexOf(migrationName) === -1; | ||
} | ||
|
||
private recordMigration(migrationName: string): void { | ||
const pastMigrations = this.state.get('migrations', '').split(';'); | ||
pastMigrations.push(migrationName); | ||
this.state.update('migrations', pastMigrations.join(';')); | ||
logger.debug(`Applied Migration: ${migrationName}`); | ||
} | ||
} | ||
|
||
export const migrations = MigrationService.getInstance(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Memento } from 'vscode'; | ||
|
||
import { up as up200 } from './migrate-2.0.0'; | ||
|
||
export const extensionMigrations: Array< | ||
[string, (state: Memento, cb: (err: Error | undefined) => void) => void] | ||
> = [['2.0.0', up200]]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters