Skip to content

Commit

Permalink
Update file being read by the GUI
Browse files Browse the repository at this point in the history
The GUI must now read from the home directory. There was some complexity
in disinguishing LocalAppData from AppData that has now become simpler,
and related tests are no longer necessary.
  • Loading branch information
EduardGomezEscandell committed Nov 7, 2023
1 parent 9d647f8 commit c3d7adb
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,28 @@ void main() {
return stack;
};

// A temporary directory mocking the $env:LocalAppData directory to sandbox our agent.
Directory? tmp;
// A temporary directory mocking the $env:UserProfile directory to sandbox our agent.
Directory? tmpHome;
Directory? tmpLocalAppData;

setUpAll(() async {
await YaruTestWindow.ensureInitialized();
// Use a random place inside the build tree as the `LOCALAPPDATA` env variable for all test cases below.
tmp = await msixRootDir().createTemp('test-');
// Use a random place inside the build tree as the `USERPROFILE` env variable for all test cases below.
tmpHome = await msixRootDir().createTemp('test-');

tmpLocalAppData = Directory(p.join(tmpHome!.path, 'AppData/Local'));
await tmpLocalAppData!.create(recursive: true);

Environment(
overrides: {
'LOCALAPPDATA': tmp!.path,
'USERPROFILE': tmpHome!.path,
'LOCALAPPDATA': tmpLocalAppData!.path,
'UP4W_ALLOW_STORE_PURCHASE': '1',
},
);
});

tearDownAll(() => tmp?.delete(recursive: true));
tearDownAll(() => tmpHome?.delete(recursive: true));
group('no agent build', () {
// Verifies that a proper message is displayed when the agent cannot be run.
testWidgets(
Expand Down Expand Up @@ -82,7 +88,7 @@ void main() {
[p.basenameWithoutExtension(agentImageName)],
);
}
File(p.join(tmp!.path, 'Ubuntu Pro', 'addr')).deleteSync();
File(p.join(tmpHome!.path, 'Ubuntu Pro', 'addr')).deleteSync();
});

tearDownAll(() async {
Expand Down
1 change: 0 additions & 1 deletion gui/packages/ubuntupro/lib/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class Pro4WindowsApp extends StatelessWidget {
onGenerateTitle: (context) => AppLocalizations.of(context).appTitle,
home: Provider<AgentStartupMonitor>(
create: (context) => AgentStartupMonitor(
appName: kAppName,
addrFileName: kAddrFileName,
agentLauncher: launch,
clientFactory: defaultClient,
Expand Down
16 changes: 4 additions & 12 deletions gui/packages/ubuntupro/lib/core/agent_api_paths.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,13 @@ import 'package:path/path.dart' as p;

import 'environment.dart';

/// Provides the full path of the "[appDir]/[filename]" file
/// Provides the full path of the "[filename]" file
/// under the well known directory where the Windows Agent stores its local data.
/// Returns null if that directory location cannot be determined from the environment.
String? agentAddrFilePath(String appDir, String filename) {
// The well-known package path_provider doesn't return the LOCALAPPDATA directory
// but the APPDATA, which is usually under %USERPROFILE%/AppData/Roaming instead of
// %USERPROFILE%/AppData/Local, which is where the agent is storing the support data.
final localAppDir = Environment.instance['LOCALAPPDATA'];
String? agentAddrFilePath(String filename) {
final localAppDir = Environment.instance['USERPROFILE'];
if (localAppDir != null) {
return p.join(localAppDir, appDir, filename);
}

final userProfile = Environment.instance['USERPROFILE'];
if (userProfile != null) {
return p.join(userProfile, 'AppData', 'Local', appDir, filename);
return p.join(localAppDir, filename);
}

return null;
Expand Down
3 changes: 1 addition & 2 deletions gui/packages/ubuntupro/lib/pages/startup/agent_monitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,11 @@ typedef AgentApiCallback = FutureOr<void> Function(AgentApiClient);

class AgentStartupMonitor {
AgentStartupMonitor({
required String appName,
required String addrFileName,
required this.agentLauncher,
required this.clientFactory,
required this.onClient,
}) : _addrFilePath = agentAddrFilePath(appName, addrFileName);
}) : _addrFilePath = agentAddrFilePath(addrFileName);

final String? _addrFilePath;

Expand Down
14 changes: 3 additions & 11 deletions gui/packages/ubuntupro/test/core/agent_api_paths_part2_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import 'dart:io';

import 'package:flutter_test/flutter_test.dart';
import 'package:ubuntupro/core/agent_api_paths.dart';
import 'package:ubuntupro/core/environment.dart';

void main() {
Expand All @@ -15,15 +14,8 @@ void main() {
// that will cost only a branch on if-null. Maybe the compiler can optimize
// that away. I'm not sure.
final _ = Environment(
overrides: {'LOCALAPPDATA': Platform.environment['APPDATA']!},
overrides: {
'USERPROFILE': Platform.environment['USERPROFILE']!,
},
);

test('misleading environment', () {
const appName = 'AwesomeApp';

final dir = agentAddrFilePath(appName, 'addr')!;

expect(dir.contains('Roaming'), isTrue);
expect(dir.contains(appName), isTrue);
});
}
22 changes: 0 additions & 22 deletions gui/packages/ubuntupro/test/core/agent_api_paths_part3_test.dart

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ void main() {
final _ = Environment(overrides: {'LOCALAPPDATA': null, 'USERPROFILE': null});

test('complete failure due environment', () {
const appName = 'AwesomeApp';

final dir = agentAddrFilePath(appName, 'addr');
final dir = agentAddrFilePath('.ubuntupro');

expect(dir, isNull);
});
Expand Down
19 changes: 5 additions & 14 deletions gui/packages/ubuntupro/test/core/agent_api_paths_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,7 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:ubuntupro/core/agent_api_paths.dart';

void main() {
tearDownAll(() => File('./addr').deleteSync());
test('dir should not contain "Roaming"', () {
const appName = 'AwesomeApp';

final dir = agentAddrFilePath(appName, 'addr')!;

expect(dir.contains('Roaming'), isFalse);
expect(dir.contains('Local'), isTrue);
expect(dir.contains(appName), isTrue);
});
tearDownAll(() => File('./.ubuntupro').deleteSync());

test('read port from line', () {
const port = 56768;
Expand All @@ -37,7 +28,7 @@ void main() {
});

test('read port from addr file', () async {
const filePath = './addr';
const filePath = './.ubuntupro';
const port = 56768;
const line = '[::]:$port';
final addr = File(filePath);
Expand All @@ -59,7 +50,7 @@ void main() {
});

test('empty file', () async {
const filePath = './addr';
const filePath = './.ubuntupro';
final addr = File(filePath);
addr.writeAsStringSync('');

Expand All @@ -70,7 +61,7 @@ void main() {
});

test('access denied', () async {
const filePath = './addr';
const filePath = './.ubuntupro';
final addr = File(filePath);
addr.writeAsStringSync('');

Expand All @@ -86,7 +77,7 @@ void main() {
});

test('bad format', () async {
const filePath = './addr';
const filePath = './.ubuntupro';
const port = 56768;
const line = 'Hello World $port';
final addr = File(filePath);
Expand Down
36 changes: 16 additions & 20 deletions gui/packages/ubuntupro/test/startup/agent_monitor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,24 @@ void main() {
const kTimeout = Duration(seconds: 3);
const kInterval = Duration(milliseconds: 100);

Directory? appDir;
Directory? homeDir;

setUpAll(() async {
// Overrides the LOCALAPPDATA value to point to a temporary directory and
// Overrides the USERPROFILE value to point to a temporary directory and
// creates the agent directory inside it, where we should find the addr file.
// Returns the mocked LOCALAPPDATA value for later deletion.
final tmp = await Directory.current.createTemp();
// Returns the mocked USERPROFILE value for later deletion.
final tmpHome = await Directory.current.createTemp();

final _ = Environment(
overrides: {'LOCALAPPDATA': tmp.path},
overrides: {
'USERPROFILE': tmpHome.path,
},
);

appDir = await Directory(p.join(tmp.path, kAppName)).create();
homeDir = tmpHome;
});
tearDownAll(() async {
await appDir?.parent.delete(recursive: true);
await homeDir?.delete(recursive: true);
});

test('agent cannot start', () async {
Expand All @@ -41,7 +45,6 @@ void main() {
/// A launch request will always fail.
agentLauncher: () async => false,
clientFactory: (port) => mockClient,
appName: kAppName,
addrFileName: kAddrFileName,
onClient: (_) {},
);
Expand All @@ -59,7 +62,7 @@ void main() {
});

test('ping non responsive', () async {
writeDummyAddrFile(appDir!);
writeDummyAddrFile(homeDir!);

// Fakes a ping failure.
final mockClient = MockAgentApiClient();
Expand All @@ -69,7 +72,6 @@ void main() {
/// A launch request will always succeed.
agentLauncher: () async => true,
clientFactory: (port) => mockClient,
appName: kAppName,
addrFileName: kAddrFileName,
onClient: (_) {},
);
Expand All @@ -86,14 +88,13 @@ void main() {
});

test('format error', () async {
writeDummyAddrFile(appDir!, line: 'Hello, 45567');
writeDummyAddrFile(homeDir!, line: 'Hello, 45567');

final mockClient = MockAgentApiClient();
final monitor = AgentStartupMonitor(
/// A launch request will always succeed.
agentLauncher: () async => true,
clientFactory: (port) => mockClient,
appName: kAppName,
addrFileName: kAddrFileName,
onClient: (_) {},
);
Expand All @@ -116,7 +117,6 @@ void main() {
/// A launch request will always succeed.
agentLauncher: () async => true,
clientFactory: (port) => mockClient,
appName: kAppName,
addrFileName: kAddrFileName,
onClient: (_) {},
);
Expand All @@ -138,7 +138,7 @@ void main() {
});

test('already running with mocks', () async {
writeDummyAddrFile(appDir!);
writeDummyAddrFile(homeDir!);

final mockClient = MockAgentApiClient();
// Fakes a successful ping.
Expand All @@ -147,7 +147,6 @@ void main() {
/// A launch request will always succeed.
agentLauncher: () async => true,
clientFactory: (port) => mockClient,
appName: kAppName,
addrFileName: kAddrFileName,
onClient: (_) {},
);
Expand All @@ -170,11 +169,10 @@ void main() {
final monitor = AgentStartupMonitor(
/// A launch request will always succeed.
agentLauncher: () async {
writeDummyAddrFile(appDir!);
writeDummyAddrFile(homeDir!);
return true;
},
clientFactory: (port) => mockClient,
appName: kAppName,
addrFileName: kAddrFileName,
onClient: (_) {},
);
Expand All @@ -201,7 +199,6 @@ void main() {
return true;
},
clientFactory: (port) => mockClient,
appName: kAppName,
addrFileName: kAddrFileName,
onClient: (_) {},
);
Expand All @@ -225,11 +222,10 @@ void main() {
final monitor = AgentStartupMonitor(
/// A launch request will always succeed.
agentLauncher: () async {
writeDummyAddrFile(appDir!);
writeDummyAddrFile(homeDir!);
return true;
},
clientFactory: (port) => mockClient,
appName: kAppName,
addrFileName: kAddrFileName,
onClient: (_) async {
// This function only completes when the completer is manually set complete.
Expand Down
1 change: 0 additions & 1 deletion gui/packages/ubuntupro/test/startup/startup_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ void main() {
final app = MaterialApp(
home: Provider<AgentStartupMonitor>(
create: (context) => AgentStartupMonitor(
appName: 'app name',
addrFileName: 'anywhere',
agentLauncher: () async => true,
clientFactory: (port) =>
Expand Down

0 comments on commit c3d7adb

Please sign in to comment.