Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[native_assets_cli] Add version skew regression test back in #1839

Merged
merged 2 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions .github/workflows/native.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ jobs:
- run: dart pub get -C test_data/native_add_add_source/
if: ${{ matrix.package == 'native_assets_builder' }}

- run: dart pub get -C test_data/native_add_version_skew/
if: ${{ matrix.package == 'native_assets_builder' }}

- run: dart pub get -C test_data/native_subtract/
if: ${{ matrix.package == 'native_assets_builder' }}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:test/test.dart';

import '../helpers.dart';
import 'helpers.dart';

const Timeout longTimeout = Timeout(Duration(minutes: 5));

void main() async {
test('Test hook using an older version of native_assets_cli',
timeout: longTimeout, () async {
await inTempDir((tempUri) async {
await copyTestProjects(targetUri: tempUri);
final packageUri = tempUri.resolve('native_add_version_skew/');

// First, run `pub get`, we need pub to resolve our dependencies.
await runPubGet(
workingDirectory: packageUri,
logger: logger,
);

{
final result = await build(
packageUri,
logger,
dartExecutable,
configValidator: validateCodeAssetBuildConfig,
buildAssetTypes: [CodeAsset.type],
buildValidator: validateCodeAssetBuildOutput,
applicationAssetValidator: validateCodeAssetInApplication,
);
expect(result?.encodedAssets.length, 1);
}
});
});
}
9 changes: 9 additions & 0 deletions pkgs/native_assets_builder/test_data/manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@
- native_add_duplicate/pubspec.yaml
- native_add_duplicate/src/native_add.c
- native_add_duplicate/src/native_add.h
- native_add_version_skew/ffigen.yaml
- native_add_version_skew/hook/build.dart
- native_add_version_skew/lib/native_add.dart
- native_add_version_skew/lib/src/native_add_bindings_generated.dart
- native_add_version_skew/lib/src/native_add.dart
- native_add_version_skew/pubspec.yaml
- native_add_version_skew/src/native_add.c
- native_add_version_skew/src/native_add.h
- native_add_version_skew/test/native_add_test.dart
- native_add/ffigen.yaml
- native_add/hook/build.dart
- native_add/lib/native_add.dart
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Run with `flutter pub run ffigen --config ffigen.yaml`.
name: NativeAddBindings
description: |
Bindings for `src/native_add.h`.

Regenerate bindings with `flutter pub run ffigen --config ffigen.yaml`.
output: "lib/src/native_add_bindings_generated.dart"
headers:
entry-points:
- "src/native_add.h"
include-directives:
- "src/native_add.h"
preamble: |
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit

Suggested change
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file

This also shows a downside of yaml configs, you can't write ${DateTime.now().year} ;)

// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
comments:
style: any
length: full
ffi-native:
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:logging/logging.dart';
import 'package:native_assets_cli/native_assets_cli.dart';
import 'package:native_toolchain_c/native_toolchain_c.dart';

void main(List<String> arguments) async {
await build(arguments, (config, output) async {
final packageName = config.packageName;
final cbuilder = CBuilder.library(
name: packageName,
assetName: 'src/${packageName}_bindings_generated.dart',
sources: [
'src/$packageName.c',
],
);
await cbuilder.run(
config: config,
output: output,
logger: Logger('')
..level = Level.ALL
..onRecord.listen((record) {
print('${record.level.name}: ${record.time}: ${record.message}');
}),
);
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

export 'src/native_add.dart';
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'native_add_bindings_generated.dart' as bindings;

int add(int a, int b) => bindings.add(a, b);
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// AUTO GENERATED FILE, DO NOT EDIT.
//
// Generated by `package:ffigen`.
// ignore_for_file: type=lint
import 'dart:ffi' as ffi;

@ffi.Native<ffi.Int32 Function(ffi.Int32, ffi.Int32)>(symbol: 'add')
external int add(
int a,
int b,
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- ffigen.yaml
- hook/build.dart
- lib/native_add.dart
- lib/src/native_add_bindings_generated.dart
- lib/src/native_add.dart
- pubspec.yaml
- src/native_add.c
- src/native_add.h
- test/native_add_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: native_add
description: Sums two numbers with native code.
version: 0.1.0

publish_to: none

environment:
sdk: '>=3.3.0 <4.0.0'

dependencies:
logging: ^1.1.1
native_assets_cli: ^0.9.0
native_toolchain_c: ^0.6.0

dev_dependencies:
ffigen: ^8.0.2
lints: ^3.0.0
some_dev_dep:
path: ../some_dev_dep/
test: ^1.23.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

#include "native_add.h"

int32_t add(int32_t a, int32_t b) {
return a + b;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

#include <stdint.h>

#if _WIN32
#define MYLIB_EXPORT __declspec(dllexport)
#else
#define MYLIB_EXPORT
#endif

MYLIB_EXPORT int32_t add(int32_t a, int32_t b);
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:native_add/native_add.dart';
import 'package:test/test.dart';

void main() {
test('native add test', () {
final result = add(4, 6);
expect(result, equals(10));
});
}
26 changes: 19 additions & 7 deletions pkgs/native_assets_cli/lib/src/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ sealed class HookOutput {
HookOutput(this.json)
: version = switch (Version.parse(json.string(_versionKey))) {
final Version version => (version.major != latestVersion.major ||
version < latestVersion)
version < latestParsableVersion)
? throw FormatException(
'Only compatible versions with $latestVersion are supported '
'(was: $version).')
Expand All @@ -279,9 +279,6 @@ sealed class HookOutput {

@override
String toString() => const JsonEncoder.withIndent(' ').convert(json);

/// The version of [HookOutput].
static final Version latestVersion = Version(1, 6, 0);
}

List<Uri> _parseDependencies(List<Object?>? list) {
Expand All @@ -298,7 +295,7 @@ sealed class HookOutputBuilder {
final Map<String, Object?> json = {};

HookOutputBuilder() {
json[_versionKey] = HookOutput.latestVersion.toString();
json[_versionKey] = latestVersion.toString();
json[_timestampKey] = DateTime.now().roundDownToSeconds().toString();
json[_dependenciesKey] = [];
}
Expand Down Expand Up @@ -537,8 +534,23 @@ extension EncodedAssetLinkOutputBuilder on LinkOutputBuilder {
}
}

// The latest supported config version.
/// The latest supported config version.
///
/// We'll never bump the major version. Removing old keys from the input and
/// output is done via modifying [latestParsableVersion].
final latestVersion = Version(1, 6, 0);

// The parser can deal with configs down to this version.
/// The parser can deal with configs and outputs down to this version.
///
/// This version can be bumped when:
///
/// 1. The stable version of Dart / Flutter uses a newer version _and_ the SDK
/// constraint is bumped in the pubspec of this package to that stable
/// version. (This prevents config parsing from failing.)
/// 2. A stable version of this package is published uses a newer version, _and_
/// most users have migrated to it. (This prevents the output parsing from
/// failing.)
///
/// When updating this number, update the version_skew_test.dart. (This test
/// catches issues with 2.)
final latestParsableVersion = Version(1, 5, 0);
3 changes: 2 additions & 1 deletion pkgs/native_assets_cli/test/build_output_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// ignore_for_file: deprecated_member_use_from_same_package

import 'package:native_assets_cli/native_assets_cli_builder.dart';
import 'package:native_assets_cli/src/config.dart';
import 'package:native_assets_cli/src/utils/datetime.dart';
import 'package:test/test.dart';

Expand Down Expand Up @@ -79,7 +80,7 @@ void main() {
(e) =>
e is FormatException &&
e.message.contains(version) &&
e.message.contains(HookOutput.latestVersion.toString()),
e.message.contains(latestVersion.toString()),
)),
);
});
Expand Down
3 changes: 2 additions & 1 deletion pkgs/native_assets_cli/test/link_output_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// BSD-style license that can be found in the LICENSE file.

import 'package:native_assets_cli/native_assets_cli_builder.dart';
import 'package:native_assets_cli/src/config.dart';
import 'package:native_assets_cli/src/utils/datetime.dart';
import 'package:test/test.dart';

Expand Down Expand Up @@ -55,7 +56,7 @@ void main() {
(e) =>
e is FormatException &&
e.message.contains(version) &&
e.message.contains(HookOutput.latestVersion.toString()),
e.message.contains(latestVersion.toString()),
)),
);
});
Expand Down
Loading