Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

[path_provider_linux] Using TMPDIR env as a primary temporary path #4218

Merged
merged 8 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/path_provider/path_provider_linux/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.1.0

* Now `getTemporaryPath` returns the value of the `TMPDIR` environment variable primarily. If `TMPDIR` is not set, `/tmp` is returned.

## 2.0.2

* Updated installation instructions in README.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:path/path.dart' as path;
import 'package:path_provider_platform_interface/path_provider_platform_interface.dart';
import 'package:xdg_directories/xdg_directories.dart' as xdg;
Expand All @@ -12,14 +13,39 @@ import 'package:xdg_directories/xdg_directories.dart' as xdg;
///
/// This class implements the `package:path_provider` functionality for linux
class PathProviderLinux extends PathProviderPlatform {
/// Constructs an instance of [PathProviderLinux]
PathProviderLinux() : _environment = Platform.environment;

/// Constructs an instance of [PathProviderLinux] with the given [environment]
@visibleForTesting
PathProviderLinux.private({
required Map<String, String> environment,
}) : _environment = environment;

final Map<String, String> _environment;

/// Registers this class as the default instance of [PathProviderPlatform]
static void registerWith() {
PathProviderPlatform.instance = PathProviderLinux();
}

/// Registers this class as the default instance of [PathProviderPlatform]
/// with the given [environment]
@visibleForTesting
static void privateRegisterWith({
required Map<String, String> environment,
}) {
PathProviderPlatform.instance = PathProviderLinux.private(
environment: environment,
);
}

@override
Future<String?> getTemporaryPath() {
return Future<String?>.value('/tmp');
final String? tmpdir = _environment['TMPDIR'];
return Future<String?>.value(
tmpdir == null || tmpdir.isEmpty ? '/tmp' : tmpdir,
);
}

@override
Expand Down
2 changes: 1 addition & 1 deletion packages/path_provider/path_provider_linux/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: path_provider_linux
description: Linux implementation of the path_provider plugin
repository: https://github.com/flutter/plugins/tree/master/packages/path_provider/path_provider_linux
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+path_provider%22
version: 2.0.2
version: 2.1.0

environment:
sdk: ">=2.12.0 <3.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ import 'package:path_provider_platform_interface/path_provider_platform_interfac

void main() {
TestWidgetsFlutterBinding.ensureInitialized();
PathProviderLinux.registerWith();

late Map<String, String> environment;

setUp(() {
environment = <String, String>{};
PathProviderLinux.privateRegisterWith(
environment: environment,
);
});

test('registered instance', () {
expect(PathProviderPlatform.instance, isA<PathProviderLinux>());
Expand All @@ -16,6 +24,12 @@ void main() {
test('getTemporaryPath', () async {
final PathProviderPlatform plugin = PathProviderPlatform.instance;
expect(await plugin.getTemporaryPath(), '/tmp');

environment['TMPDIR'] = '';
expect(await plugin.getTemporaryPath(), '/tmp');

environment['TMPDIR'] = '/run/user/0/tmp';
expect(await plugin.getTemporaryPath(), '/run/user/0/tmp');
});

test('getApplicationSupportPath', () async {
Expand Down