88
88
import com .oracle .truffle .api .InternalResource ;
89
89
import com .oracle .truffle .api .test .ReflectionUtils ;
90
90
import org .graalvm .collections .Pair ;
91
- import org .graalvm .nativeimage .ImageInfo ;
92
91
import org .graalvm .options .OptionCategory ;
93
92
import org .graalvm .options .OptionDescriptor ;
94
93
import org .graalvm .options .OptionDescriptors ;
@@ -2588,7 +2587,7 @@ public void testSandboxPolicyLanguageOptionFailure() throws Exception {
2588
2587
@ Test
2589
2588
@ SuppressWarnings ("try" )
2590
2589
public void testLanguageInternalResources () throws Exception {
2591
- Assume . assumeFalse ( "Cannot run as native unittest" , ImageInfo . inImageRuntimeCode () );
2590
+ TruffleTestAssumptions . assumeNotAOT ( );
2592
2591
setPatchable (FIRST );
2593
2592
List <TruffleFile > files = new ArrayList <>();
2594
2593
try (TemporaryResourceCacheRoot imageBuildTimeCacheRoot = new TemporaryResourceCacheRoot (false )) {
@@ -2607,6 +2606,35 @@ public void testLanguageInternalResources() throws Exception {
2607
2606
}
2608
2607
}
2609
2608
2609
+ @ Test
2610
+ @ SuppressWarnings ("try" )
2611
+ public void testInternalResourcesInNonPreInitializedEngine () throws Exception {
2612
+ TruffleTestAssumptions .assumeNotAOT ();
2613
+ setPatchable (FIRST );
2614
+ List <TruffleFile > files = new ArrayList <>();
2615
+ try (TemporaryResourceCacheRoot imageBuildTimeCacheRoot = new TemporaryResourceCacheRoot (false )) {
2616
+ BaseLanguage .registerAction (ContextPreInitializationTestFirstLanguage .class , ActionKind .ON_INITIALIZE_CONTEXT , newResourceBuildTimeVerifier (files ));
2617
+ doContextPreinitialize (FIRST );
2618
+ assertFalse (files .isEmpty ());
2619
+ }
2620
+ Path runtimeCacheRoot = Files .createTempDirectory (null ).toRealPath ();
2621
+ Engine .copyResources (runtimeCacheRoot , FIRST );
2622
+ System .setProperty ("polyglot.engine.resourcePath" , runtimeCacheRoot .toString ());
2623
+ try (Engine engine = Engine .create ()) {
2624
+ BaseLanguage .registerAction (ContextPreInitializationTestFirstLanguage .class , ActionKind .ON_PATCH_CONTEXT , (env ) -> {
2625
+ throw CompilerDirectives .shouldNotReachHere ("Pre-initialized context should not be used." );
2626
+ });
2627
+ BaseLanguage .registerAction (ContextPreInitializationTestFirstLanguage .class , ActionKind .ON_INITIALIZE_CONTEXT , newResourceNonPreInitializedContextVerifier (runtimeCacheRoot .toString ()));
2628
+ try (Context ctx = Context .newBuilder ().engine (engine ).build ()) {
2629
+ Value res = ctx .eval (Source .create (FIRST , "test" ));
2630
+ assertEquals ("test" , res .asString ());
2631
+ }
2632
+ } finally {
2633
+ System .getProperties ().remove ("polyglot.engine.resourcePath" );
2634
+ TemporaryResourceCacheRoot .reset (false );
2635
+ }
2636
+ }
2637
+
2610
2638
private static Consumer <Env > newResourceBuildTimeVerifier (List <TruffleFile > files ) {
2611
2639
return (env ) -> {
2612
2640
try {
@@ -2624,6 +2652,25 @@ private static Consumer<Env> newResourceBuildTimeVerifier(List<TruffleFile> file
2624
2652
};
2625
2653
}
2626
2654
2655
+ private static Consumer <Env > newResourceNonPreInitializedContextVerifier (String expectedRootPrefix ) {
2656
+ return (env ) -> {
2657
+ try {
2658
+ ContextPreInitializationResource .unpackCount = 0 ;
2659
+ TruffleFile root = env .getInternalResource (ContextPreInitializationResource .class );
2660
+ assertEquals (0 , ContextPreInitializationResource .unpackCount );
2661
+ assertNotNull (root );
2662
+ assertTrue (root .isAbsolute ());
2663
+ TruffleFile resource = root .resolve (ContextPreInitializationResource .FILE_NAME );
2664
+ assertNotNull (resource );
2665
+ assertTrue (resource .isAbsolute ());
2666
+ assertTrue (resource .getAbsoluteFile ().toString ().startsWith (expectedRootPrefix ));
2667
+ assertEquals (ContextPreInitializationResource .FILE_CONTENT , new String (resource .readAllBytes (), StandardCharsets .UTF_8 ));
2668
+ } catch (IOException ioe ) {
2669
+ throw new AssertionError (ioe );
2670
+ }
2671
+ };
2672
+ }
2673
+
2627
2674
private static Consumer <Env > newResourceExecutionTimeVerifier (List <TruffleFile > files , String expectedRootPrefix ) {
2628
2675
return (env ) -> {
2629
2676
try {
@@ -2651,7 +2698,7 @@ private static Consumer<Env> newResourceExecutionTimeVerifier(List<TruffleFile>
2651
2698
@ Test
2652
2699
@ SuppressWarnings ("try" )
2653
2700
public void testSourcesForInternalResources () throws Exception {
2654
- Assume . assumeFalse ( "Cannot run as native unittest" , ImageInfo . inImageRuntimeCode () );
2701
+ TruffleTestAssumptions . assumeNotAOT ( );
2655
2702
setPatchable (FIRST );
2656
2703
List <com .oracle .truffle .api .source .Source > sources = new ArrayList <>();
2657
2704
try (TemporaryResourceCacheRoot imageBuildTimeCacheRoot = new TemporaryResourceCacheRoot (false )) {
@@ -2693,7 +2740,7 @@ public void testSourcesForInternalResources() throws Exception {
2693
2740
@ Test
2694
2741
@ SuppressWarnings ("try" )
2695
2742
public void testInstrumentInternalResources () throws Exception {
2696
- Assume . assumeFalse ( "Cannot run as native unittest" , ImageInfo . inImageRuntimeCode () );
2743
+ TruffleTestAssumptions . assumeNotAOT ( );
2697
2744
setPatchable (FIRST );
2698
2745
AtomicReference <TruffleFile > rootRef = new AtomicReference <>();
2699
2746
ContextPreInitializationFirstInstrument .actions = Collections .singletonMap ("onContextCreated" , (e ) -> {
@@ -2728,7 +2775,7 @@ public void testInstrumentInternalResources() throws Exception {
2728
2775
@ Test
2729
2776
@ SuppressWarnings ("try" )
2730
2777
public void testOverriddenCacheRoot () throws Exception {
2731
- Assume . assumeFalse ( "Cannot run as native unittest" , ImageInfo . inImageRuntimeCode () );
2778
+ TruffleTestAssumptions . assumeNotAOT ( );
2732
2779
setPatchable (FIRST );
2733
2780
List <TruffleFile > files = new ArrayList <>();
2734
2781
try (TemporaryResourceCacheRoot imageBuildTimeCacheRoot = new TemporaryResourceCacheRoot (false )) {
@@ -2756,7 +2803,7 @@ public void testOverriddenCacheRoot() throws Exception {
2756
2803
@ Test
2757
2804
@ SuppressWarnings ("try" )
2758
2805
public void testOverriddenComponentRoot () throws Exception {
2759
- Assume . assumeFalse ( "Cannot run as native unittest" , ImageInfo . inImageRuntimeCode () );
2806
+ TruffleTestAssumptions . assumeNotAOT ( );
2760
2807
setPatchable (FIRST );
2761
2808
List <TruffleFile > files = new ArrayList <>();
2762
2809
try (TemporaryResourceCacheRoot imageBuildTimeCacheRoot = new TemporaryResourceCacheRoot (false )) {
@@ -2783,7 +2830,7 @@ public void testOverriddenComponentRoot() throws Exception {
2783
2830
@ Test
2784
2831
@ SuppressWarnings ("try" )
2785
2832
public void testOverriddenResourceRoot () throws Exception {
2786
- Assume . assumeFalse ( "Cannot run as native unittest" , ImageInfo . inImageRuntimeCode () );
2833
+ TruffleTestAssumptions . assumeNotAOT ( );
2787
2834
setPatchable (FIRST );
2788
2835
List <TruffleFile > files = new ArrayList <>();
2789
2836
try (TemporaryResourceCacheRoot imageBuildTimeCacheRoot = new TemporaryResourceCacheRoot (false )) {
0 commit comments