File tree Expand file tree Collapse file tree 2 files changed +42
-3
lines changed
packages/compass-preferences-model/src Expand file tree Collapse file tree 2 files changed +42
-3
lines changed Original file line number Diff line number Diff line change 1
1
import { mkdtempSync , rmdirSync } from 'fs' ;
2
2
import path from 'path' ;
3
3
import os from 'os' ;
4
- import { UserStorage , type User } from './storage' ;
4
+ import { UserStorage , StoragePreferences , type User } from './storage' ;
5
5
import { expect } from 'chai' ;
6
+ import type { UserPreferences } from './preferences' ;
6
7
7
8
describe ( 'storage' , function ( ) {
8
9
let tmpDir : string ;
@@ -41,4 +42,28 @@ describe('storage', function () {
41
42
} ) ;
42
43
} ) ;
43
44
} ) ;
45
+
46
+ describe ( 'StoragePreferences' , function ( ) {
47
+ before ( function ( ) {
48
+ tmpDir = mkdtempSync ( path . join ( os . tmpdir ( ) ) ) ;
49
+ } ) ;
50
+
51
+ after ( function ( ) {
52
+ rmdirSync ( tmpDir , { recursive : true } ) ;
53
+ } ) ;
54
+
55
+ it ( 'will strip unknown prefs' , async function ( ) {
56
+ const prefsStorage = new StoragePreferences (
57
+ { showedNetworkOptIn : true , foo : true } as unknown as UserPreferences ,
58
+ tmpDir
59
+ ) ;
60
+ await prefsStorage . setup ( ) ;
61
+
62
+ // roundabout way to make it load because setup doesn't fill prefsStorage.preferences if it writes the file
63
+ await prefsStorage . updatePreferences ( { } ) ;
64
+
65
+ const prefs = prefsStorage . getPreferences ( ) ;
66
+ expect ( prefs ) . to . deep . equal ( { showedNetworkOptIn : true } ) ;
67
+ } ) ;
68
+ } ) ;
44
69
} ) ;
Original file line number Diff line number Diff line change
1
+ import { UUID } from 'bson' ;
1
2
import { promises as fs } from 'fs' ;
3
+ import { pick } from 'lodash' ;
2
4
import { join } from 'path' ;
3
5
import type { UserPreferences } from './preferences' ;
4
- import { UUID } from 'bson ' ;
6
+ import { allPreferencesProps } from './preferences ' ;
5
7
6
8
export abstract class BasePreferencesStorage {
7
9
abstract setup ( ) : Promise < void > ;
@@ -71,7 +73,19 @@ export class StoragePreferences extends BasePreferencesStorage {
71
73
}
72
74
73
75
private async readPreferences ( ) : Promise < UserPreferences > {
74
- return JSON . parse ( await fs . readFile ( this . getFilePath ( ) , 'utf8' ) ) ;
76
+ const preferences = JSON . parse (
77
+ await fs . readFile ( this . getFilePath ( ) , 'utf8' )
78
+ ) ;
79
+
80
+ // Exclude old and renamed preferences so we don't try and save those back.
81
+ // (This is still technically a lie because we could only have a subset of
82
+ // UserPreferences. ie. imagine a JSON file that has an empty object. Or
83
+ // that was saved with the previous version of compass where we had fewer
84
+ // settings.)
85
+ return pick (
86
+ preferences ,
87
+ Object . keys ( allPreferencesProps )
88
+ ) as UserPreferences ;
75
89
}
76
90
77
91
getPreferences ( ) {
You can’t perform that action at this time.
0 commit comments