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 11import { mkdtempSync , rmdirSync } from 'fs' ;
22import path from 'path' ;
33import os from 'os' ;
4- import { UserStorage , type User } from './storage' ;
4+ import { UserStorage , StoragePreferences , type User } from './storage' ;
55import { expect } from 'chai' ;
6+ import type { UserPreferences } from './preferences' ;
67
78describe ( 'storage' , function ( ) {
89 let tmpDir : string ;
@@ -41,4 +42,28 @@ describe('storage', function () {
4142 } ) ;
4243 } ) ;
4344 } ) ;
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+ } ) ;
4469} ) ;
Original file line number Diff line number Diff line change 1+ import { UUID } from 'bson' ;
12import { promises as fs } from 'fs' ;
3+ import { pick } from 'lodash' ;
24import { join } from 'path' ;
35import type { UserPreferences } from './preferences' ;
4- import { UUID } from 'bson ' ;
6+ import { allPreferencesProps } from './preferences ' ;
57
68export abstract class BasePreferencesStorage {
79 abstract setup ( ) : Promise < void > ;
@@ -71,7 +73,19 @@ export class StoragePreferences extends BasePreferencesStorage {
7173 }
7274
7375 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 ;
7589 }
7690
7791 getPreferences ( ) {
You can’t perform that action at this time.
0 commit comments